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