Home  >  Article  >  Web Front-end  >  Create a nodejs server easily (1): A simple nodejs server example_node.js

Create a nodejs server easily (1): A simple nodejs server example_node.js

WBOY
WBOYOriginal
2016-05-16 16:25:551012browse

Let’s implement a simple example first, hello world.

It seems that the first section of every language tutorial talks about this, and we are no exception.

First we create a project directory. The directory can be defined by yourself. The directory in this case is e:/nodetest/.

Since we are building a server, I named the first file server.js.

Enter the following code in server.js:

Copy code 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 navigate to the project directory, and then execute the node server.js command to run the file;

Then open the browser and visit http://localhost:8888/, you will see a webpage that says "Hello World";

In fact, this is a simple working server, but it is too simple to do anything. But it doesn’t matter, follow me step by step, and I will teach you how to build a complete and usable server.

In the next section, we will analyze the composition of this code.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn