Node. js builds local servers to easily solve cross-origin problems. node. js local servers

Source: Internet
Author: User
Tags node server

Node. js builds local servers to easily solve cross-origin problems. node. js local servers

Recently, I used a demo written with jquery to run it. At the beginning, I forgot to enable the local service. As a result, the console always reported XMLHttpRequest cannot load file: // C: /Users/79883/Desktop/ajax/data. json? {% 22 username % 22: % 22lcl@qq.com % 22, % 22pwd % 22: % 22Home20170702% 22 }. cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https .. cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. then I start my own server. in this process, I felt that this method was quite troublesome. I tried other methods to modify the browser attributes, but I did not solve the cross-origin problem, two effective methods are obtained. Here I Let's introduce these two methods together.

1. Use Node to create a Web server

Note: Node. js provides the http module, which is mainly used to build http servers and clients. The following is a simple server implementation process:

1. Write server code server. js

Var http = require ('http'); var fs = require ('fs'); // introduce the file reading module var documentRoot = 'C: /Users/79883/Desktop/jquery/ajax; // directory of the file to be accessed var server = http. createServer (function (req, res) {// url entered by the client. For example, if you enter localhost: 8888/index.html //, url =/index.html var url = req. url; var file = documentRoot + url; console. log (url); fs. readFile (file, function (err, data) {/* The first parameter is the file path, the second parameter is the callback function, and the first parameter is the information returned by the read error, if the returned result is null, no error occurs. The two parameters are the Content of the successfully returned text */if (err) {// HTTP Status Code 404: not found // Content Type: text/plain res. writeHeader (404, {'content-type': 'text/html; charset = "UTF-8" '}); res. write ('

Through the code above, we can find files on the server. Below, we will create an html file and then access it through a browser.

2. html file (index.html) for browser requests

<! DOCTYPE html> 

3. Test

(1) first, start the server, open cmd, find the project location, and then enter the command node server. js to start the server.

(2) access in the browser. In the url bar, enter http: // 127.0.0.1: 888/index.html

If the corresponding page is displayed, it indicates that you have succeeded. Next, I will introduce nodejs to quickly build a local service, that is, the second method I mentioned.

Ii. Use nodejs to quickly build a local service

Note: node. js anywhere is to change your current directory to the root directory of a static file server anytime, anywhere.

1. Open cmd and enter node-v to check whether you have installed node. js, and then enter the command npm install angwhere-g to install the static file server. After installation, the following figure is displayed:

2. Find the path of the server you want to build on the cmd page.

3. Enter "anywhere 8888" in the current path, as shown in figure.

4. Press enter and the browser will automatically open the local access URL. A simple node server will be set up like this!

Summary

The above section describes how to set up a local server for nodejs to easily solve cross-origin problems. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.