This time I will bring you nodejs to build a local server to handle cross-domain, and nodejs to build a local server to handle cross-domain. What are the precautions?The following is a practical case, let's take a look.
Recently I took out a small demo written with jquery and ran it. At the beginning, I forgot to enable the local service, causing the console to keep reporting XMLHttpRequest cannot load file:///C :/Users/79883/Desktop/ajax/data.json?{"username":"lcl@qq.com","pwd":"Home20170702"}. 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 started writing it myself server.js server, during the process, I felt that this method was quite troublesome, so I kept trying other methods, such as changing browser properties, etc., but none of them solved the cross-domain problem. In the end, I came to two conclusions: This is an effective way. Here I will introduce these two methods to you.
1. Use Node to create a Web server
Note: Node.js provides the http module, which is mainly used to build HTTP servers. and client, the following is a simple server implementation process:
1. Write the server code server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Through the above code, we can implement the server for files Search, below, we will create an html file, and then access it through the browser.
2. Write the html file (index.html) for the browser to make requests
1 2 3 4 5 6 7 8 9 10 11 |
|
3. Test
(1) First we start the server, open cmd, find the location of the project, and then enter the command node server.js to start the server
(2) To access in the browser, enter http:/ in the url bar /127.0.0.1:888/index.html
If the corresponding page is displayed, it proves that you are successful. Next, I will introduce nodejs to quickly build local services, which is the second method I mentioned.
2. Use nodejs to quickly build local services
Note: node.js’ anywhere is to turn your current directory into a The root directory of the static file server
1. First open cmd, 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. Install The picture is shown below:
2. Find the path you want to build the server on the cmd page
3, and then enter in the current path: anywhere 8888 as follows The picture shows
4. After pressing Enter, the browser will automatically open the local access URL, and a simple node server has been set up by us!
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
A case description of the use of the filter() method in jquery
Detailed explanation of the use of the filter() method in jquery
The above is the detailed content of nodejs builds a local server to handle cross-domain. For more information, please follow other related articles on the PHP Chinese website!