NodeJS creates the simplest HTTP server and nodejs creates the server

Source: Internet
Author: User

NodeJS creates the simplest HTTP server and nodejs creates the server

☆Introduction

var http = require('http');http.createServer(function(request, response){  response.writeHead(200, { 'Content-Type': 'text-plain' });  response.end('Hello World\n');}).listen(8124);

There is a. createServer server method for the top-level http object.
A server is created, and two events related to the server are created.
Request event, request event, and request object.
Response event, response event, and responose object.
Therefore, in this function, the first parameter is the request object, and the second parameter is the response object.

☆Http request

1. http request event-request event

HTTP request information, which is sent by the http. request event.
Events are objects, and objects are attributes and methods.

What is the request information sent by this request event?
Is this object -- http. ServerRequest.
Objects are attributes and methods.

2. Attributes of http. ServerRequest

The property of the request object is:


The http. ServerRequest object describes the request information, which consists of two parts:
1. Request Header)
2. Request body)
The data in the request header is short in length and can be read immediately after resolution.
The request body may take a relatively long time to transmit.

3. http. ServerRequest related events

There are four events related to the request information sent by http. ServerRequest,
See the following table:

HTTP Response

1. http response event-response event

The server receives http. ServerRequest-the request data sent from the client to the server. It processes the data and sends a response to the client.
The response event is responce, which is the second parameter on the server.
There are several methods related to this response:

2. attributes of the response object

This message sent by the response event is the http. ClientResponse object.
This object describes the response information,
The response information is composed of two parts.
1. Response Header)
2. Response body)

The attributes of this object are described in the following table:

3. http. ClientResponse related events

These events are also related to the response information.

☆Introduction code

// Introduce the built-in http module var http = require ('http'); // 2http. createServer (function (request, response) {response. writeHead (200, {'content-type': 'text-plain '}); response. end ('Hello World \ n ');}). listen (0, 8124 );

In the Code marked with 2 in the annotation, it indicates that there is a createServer method in http. The function parameters are two. The first is the request event, and the second is the response event.
Inside this function body?
Response. writeHead is a method for writing a response header,
1. 200 is an HTTP status code, indicating that the request is successfully processed,
2. {'content-type': 'text-plain '} is a MIME standard format description.
Tell the browser what the returned content type is, and text-plain is the MIME type.
3. response. end () indicates the end of data transmission in the response event. The data event is not triggered because the request does not send the request body data.

http.createServer(function(request,response){}).listen(8124);

This is the simplest server. The listening port is port 8124 on the server.

Step 1:Save the code in the Guide as a. js file and run the js file with Node.

:

Step 2:When the browser accesses the server port (local port 8124), the response information returned by the server program is displayed.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.