Home > Backend Development > PHP Tutorial > Nodejs creates the first application

Nodejs creates the first application

WBOY
Release: 2016-07-29 09:01:34
Original
962 people have browsed it


Node.js creates the first application

If we use PHP to write back-end code, we need an Apache or Nginx HTTP server, coupled with the mod_php5 module and php-cgi.

From this perspective, the entire need of "receiving HTTP requests and serving Web pages" does not require PHP to handle it at all.

But for Node.js, the concept is completely different. When using Node.js, we are not only implementing an application, but also an entire HTTP server. In fact, our web application and corresponding web server are basically the same.

Before we create the first "Hello, World!" application in Node.js, let us first understand what parts the Node.js application consists of:

  1. Introducing the required module: We can use the require directive to load the Node.js module.

  2. Create a server: The server can listen to client requests, similar to HTTP servers such as Apache and Nginx.

  3. Receiving requests and responding to requests The server is easy to create. The client can use a browser or terminal to send HTTP requests, and the server returns response data after receiving the request.


Create a Node.js application

Step 1. Introduce the required module

We use the require directive to load the http module and assign the instantiated HTTP value to the variable http. The example is as follows:

<span>var</span><span> http </span><span>=</span><span>require</span><span>(</span><span>"http"</span><span>);</span>
Copy after login

Step 1. Create a server

Next we use the http.createServer() method to create a server and use the listen method to bind port 8888. Functions receive and respond to data through request and response parameters.

The example is as follows. Create a file called server.js in the root directory of your project and write the following code:

<span>var</span><span> http </span><span>=</span><span>require</span><span>(</span><span>'http'</span><span>);</span>
Copy after login

http.createServer(function(request , response){//Send HTTP header// HTTP status value: 200: OK// Content type: text/plain
response
.onwritehead (200, {'Content-Type': 'text/plain'}); // Send the response data "Hello" hello World" response
.
end('Hello Worldn');}).listen(8888);// The terminal prints the following information console
.
log('Server running at http://127.0.0.1:8888/');With the above code we have completed a working HTTP server.

Use the node command to execute the above code:

<span>node server</span><span>.</span><span>js <br></span><span>Server</span><span> running at http</span><span>:</span><span>//127.0.0.1:8888/</span>
Copy after login

Nodejs 创建第一个应用Next, open the browser and visit http://127.0.0.1:8888/, you will see a web page that says "Hello World".

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i

').text(i)); }; $numbering.fadeIn(1700); }); }); The above introduces the creation of the first application in Nodejs, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template