On node. JS http

Source: Internet
Author: User

In the previous example, a simple example of using the HTTP module has been written

var http = require (' http '); Request HTTP Module

var func = function (req,res) {

Res.writehead (200,{' content-type ': ' Text/plain ');//corresponding header 200 indicates normal return
Res.write ("node. js")
Res.end ();

}//web handler function when the server receives the request

var server = Http.createserver (func);//Create Web server object

Server.listen (3000);
Console.log ("Server is on");

Below we can add simple routes (different access paths) based on a simple HTTP server to respond to different requests

var http = require (' http ');
var url = require (' URL ');

Http.createserver (function (req,res) {

var pathname = Url.parse (req.url). Pathname;

if (pathname = = = '/') {
Res.writehead ($, {' Content-type ': ' Text-plain '});
Res.end (' home page ');

} else if (pathname = = = '/redirect ') {
Res.writehead (301, {
' Location ': ' http://www.baidu.com '
});
Res.end ();
} else {
Res.writehead (404, {
' Content-type ': ' Text/plain '
});
Res.end (' Page not found ');
}
}). Listen (3000, "127.0.0.1");
Console.log (' Server is on ');

We can handle 3 different situations when normal return, redirect, page not found. node. JS encapsulates the operation of adding a route for us, and this is just a way to understand how it works.

Using Express

Create a Basic Express site

Install Express

NPM install-g express//-g for global installation You can use the installed module in any location on the file system relative to the global installation local installation is to install the module under a Node_modules folder in the project for the project to use

Go to project directory via CMD to run Express express_example

Enter the dependencies required to install Express in the Express_example directory

The directory for the project is like this

Open App.js file Add the following phrase to the end of the file

Indicates that you can access our example via port 3000 (boot server)

Run App.js

Here's what the next Express auto-generated file is used for

App.js is used to start the application, which contains configuration information for the application

Package.json provides information about the application, including the dependencies that are required to run the application (this is where we add the dependency module directly in Package.json and then NPM Install or our manual one installation module Recommended Direct modification Package.json run NPM Install this simplifies our operations, and other developers can use our program very well.

Node_modules Save the node module that is defined in the Package.json and is already installed

Public

In fact, looking at the public directory can roughly guess its purpose it can provide resources for our page, style

Routers this defines the routing

View defines the layout of the application (template, using the template engine allows us to show different content through a template similar to the blog everyone's homepage, unified management)

Add a route in the project we build

Routing: Describes whether and how an application responds to a specific Hypertext Transfer Protocol request

Express uses HTTP verbs to define routes, which are commonly used

Get data from the server

Post sends data to the server

(not finished yet)

On node. JS http

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.