How to Use http. createServer in node. js _ node. js

Source: Internet
Author: User
This article mainly introduces node. http. the createServer method is described in this article. createServer method description, syntax, receiving parameters, use instances and implementation source code. For more information, see Method description:

This function is used to create an HTTP server and use requestListener as the listener function for request events.

Syntax:

The Code is as follows:


Http. createServer ([requestListener])

Because this method belongs to the http module, the http module (var http = require ("http") must be introduced before use "))

Receiving parameters:

The requestListener request processing function is automatically added to the request event. The function passes two parameters:

The req request object. If you want to know the attributes of req, you can view "http. request attribute integration ".

Res response object. The response to be made after receiving the request. For details about attributes of res, refer to "http. response attribute integration ".

Example:

In this example, res specifies the response header, and the response body content is node. js. end with end.

Finally, call the listen function to listen to port 3000.

The Code is as follows:


Var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'text/html '});
Res. write ('node. js ');
Res. end ('

Hello World

');
}). Listen (3000 );

Source code:

The Code is as follows:


Exports. createServer = function (requestListener ){
Return new Server (requestListener );
};

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.