This article mainly introduces a simple node. js server example. This article implements a simple helloworld example and shows how to run this server, for more information, see the following example: hello world.
It seems that the first section of each language tutorial will talk about this, and we are no exception.
First, create a project directory, which can be defined by yourself. In this case, the directory is e:/nodetest /.
Since we are building a server, I name the first file server. js.
Enter the following code in server. js:
The Code is as follows:
Var http = require ("http ");
Http. createServer (function (request, response ){
Response. writeHead (200, {"Content-Type": "text/plain "});
Response. write ("Hello World ");
Response. end ();
}). Listen (8888 );
Then we open cmd.
Use cd e:/nodetest/to locate the project directory, and then run the node server. js command to run the file;
Then open the browser to access http: // localhost: 8888/, and you will see a webpage with "Hello World;
In fact, this is a simple and workable server, but nothing can be done, but it doesn't matter. Follow me step by step, I will teach you how to build a complete and available server.
In the next section, we will analyze the composition of this Code.