Current location - Quotes Website - Team slogan - Unable to connect to the local server to use Nodejs, how to solve it?
Unable to connect to the local server to use Nodejs, how to solve it?
To build a nodejs server:

1. Install nodejs service (downloaded and installed in official website), and node is equivalent to apache server.

2. Create a new server file in your own defined directory, such as server.js.

For example, I created the server.js file under E:\PhpProject\html5\websocket.

var http = require(' http '); //Introduce the http module//Start the service, and the listening port is 8888// The port number is preferably above 6000. Varserver = http。 createserver (function (req,res) {

/*

Req is used to accept client data.

Res is used to send server data to the client.

*/

Console.log ('with client connection'); //The successfully created connection is displayed in the background.

//One parameter is the http request status, and 200 connection is successful.

//Write header information to the client after the connection is successful.

res.writeHeader(200,{ ' content-type ':' text/html; charset="utf-8 " '

});

Res.write ('this is the body part'); //Show to the client

RES . end();

). Listen (8888);

Console.log ("Server started successfully"); 123456789 10 1 1 12 13 14 15 16 17 18 19202 1222324

3. In the cmd console, switch the cd to the directory where server.js is located, and then execute the node server.js command.

When the console displays "Server started successfully", it means that the node server has been established.

4. Access the server in a browser.

Enter in the browser.

Localhost:8888, the browser displays "This is the body part".

Look at the cmd console and it shows "Client Connected".

You can do this in multiple browser windows, and each browser window will correspond to a Client Connection.

The above steps are completed, and the node service construction is completed. The following is how to access the text/html text file of the local site through the node service.

Access local site files

1. Create the node service file server2.js in the custom directory.

var http = require(' http '); var fs = require(' fs '); //Introduce the file reading module Var Document root =' e:/PHP Project/HTML5/WebSocket/www'; //The storage directory of the file to be accessed var server = http.createserver (function (req, res) {

var url = req.url

//url entered by the client, for example, if you enter localhost: 8888/index.html

//So the URL here is = =/index.html.

var file = document root+URL;

console . log(URL); //E:/PHP project/html 5/web socket/www/index . html

Fs.readFile (file, function (error, data) {

/*

One parameter is the file path.

The two parameters are callback functions.

One parameter of the callback function reads the information returned by the error. If it is empty, there is no error.

The second parameter is the text content returned after reading successfully.

*/

If (error) {

res.writeHeader(404,{ ' content-type ':' text/html; charset="utf-8 " '

});

RES . write(' & lt; h 1 & gt; 404 error

RES . end();

} Otherwise {

res.writeHeader(200,{ ' content-type ':' text/html; charset="utf-8 " '

});

Res.write (data); //Display index.html on the client.

RES . end();

}

});

). Listen (8888);

Console.log ('server started successfully'); 123456789 10 1 1 12 13 14 15 16 17 18 19202 1 2223242526272829303 13233343536373839404 14243444546

2. Create a index.html file

If you want to access the index.html file, you must have this file first, otherwise the server will fail to read it and return to 404 12.

3. Switch the cmd console cd to the server2.js directory and execute the nodeserver2.js command.

Open the server

4. Enter localhost:8888/index.html in the browser to access the file.