Home >Web Front-end >JS Tutorial >Node.js practice building a simple web server_javascript skills

Node.js practice building a simple web server_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:55:32998browse

In the previous chapter, we introduced Node.js, a JavaScript server platform for Internet services. At the same time, the running environment of Node.js has been set up, and the basic functions of Node.js have been verified through two HelloWorld programs. In this chapter, we also use Node.js to build a simple web server through practical exercises.

If you are familiar with web development on .NET or other similar platforms, you may be like, what is the point of setting up a web server? Just create a web project in Visual Studio and click to run. This is indeed the case, but please don’t forget that the cost is that, for example, if you use .NET to develop a Web application, you use a complete IIS as the basis of your Web server, so that when your application is released, it will I can only use IIS. And if you use a stand-alone server (built by yourself using System.Web.Hosting), you have to deal with various HttpListeners and corresponding threads, which will be more troublesome. After all, .NET is not focused on the Web. Node.js provides a convenient and customizable approach in this regard, on which you can build a sophisticated service platform that is fully oriented to your application.


1. Building a simple web server involves some basic knowledge points of Node.js:

1. Request module
in Node.js , the system provides many useful modules (of course you can also write your own modules in JavaScript, which we will explain in detail in subsequent chapters), such as http, url, etc. Modules encapsulate specific functions and provide corresponding methods or properties. To use these modules, you need to first request the module to obtain its operation object.

For example, to use the system http module, you can write like this:

Copy the code The code is as follows:

var libHttp = require('http'); //Request the HTTP protocol module

In this way, future programs will be able to access the functions of the http module through the variable libHttp. The following system modules are used in the routines in this chapter:
http: encapsulates the server and client implementation of the http protocol;
url: encapsulates the parsing and processing of urls;
fs: encapsulates the function of file system operations ;
path: Encapsulates the path parsing function.
With these modules, we can stand on the shoulders of giants to build our own applications.
2. Console
In order to better observe the operation of the program and to easily check errors when exceptions occur, you can use the function of the console through the variable console.
Copy code The code is as follows:

console.log('This is a log message') ;
Time and output timing information on the console:
//Start timing
console.timeEnd('Timer 1'); //Start the timer named "Timer 1"
...
...
...
//End the timing and output it to the console
console.timeEnd('Timer 1'); //The end is called "timing" 1" timer and output

3. Define function
The method of defining a function in Node.js is exactly the same as in ordinary JavaScript, but our recommended writing method is as follows, that is, use a The variable is named for the function, so that it is more convenient and clear to pass the function as a parameter to other functions:
Copy the code The code is as follows:

//Define a function named showErr
var showErr=function(msg){
var inf="Error!" msg;
console.log(inf msg) ;
return msg;
}

4. Create a Web server and listen for access requests
The most important thing about creating a Web server is to provide a response function for Web requests. It has two Parameters, the first represents the information requested by the client, and the other represents the information to be returned to the client. In the response function, the request information should be parsed, and the returned content should be assembled according to the request.
Copy code The code is as follows:

//Request module
var libHttp = require( 'http'); //HTTP protocol module
//Web server main function, parses requests, returns Web content
var funWebSvr = function (req, res){
res.writeHead(200, {' Content-Type': 'text/html'});
res.write('');
res.write('

*** Node.js ***

');
res.write('

Hello!

');
res.end('}
//Create an http server
var webSvr=libHttp.createServer(funWebSvr);
//Start listening to port 8124
webSvr.listen(8124);

5. Parse Web requests
For simple Web page access requests, important information is contained in the URL of the request information parameters. We can use the URL parsing module to parse the access path in the URL, and use the path module to The access path is assembled into the actual file path to be accessed for return.
Copy code The code is as follows:

var reqUrl=req.url; //Get the requested url
//Output the requested path to the console
console.log(reqUrl);
//Use the url parsing module to obtain the path name in the url
var pathName = libUrl.parse(reqUrl) .pathname;
//Use the path module to get the extension in the path name
if (libPath.extname(pathName)=="") {
//If the path has no extension
pathName = "/"; //Specify access directory
}
if (pathName.charAt(pathName.length-1)=="/"){
//If access directory
pathName ="index .html"; //Specify as the default web page
}
//Use the path parsing module to assemble the actual file path
var filePath = libPath.join("./WebRoot",pathName);

6. Set the return header
Since it is a web request, the http return header needs to be included in the return content. The focus here is to set the content type of the http return header according to the file extension of the file path to be accessed. .
Copy code The code is as follows:

var contentType="";
//Use The path parsing module obtains the file extension
var ext=libPath.extname(filePath);
switch(ext){
case ".html":
contentType= "text/html";
break;
case ".js":
contentType="text/javascript";
break;
...
...
default:
contentType= "application/octet-stream";
}
//Write the content type in the return header
res.writeHead(200, {"Content-Type": contentType });

7. Write the accessed file content into the returned object
With the actual path of the file that needs to be accessed and the content type corresponding to the file, you can use the fs file system module to read the file stream and return it to client.
Copy code The code is as follows:

//Determine whether the file exists
libPath.exists (filePath, function(exists){
if(exists){//The file exists
//Write the content type in the return header
res.writeHead(200, {"Content-Type": funGetContentType (filePath) });
//Create a read-only stream for returning
var stream = libFs.createReadStream(filePath, {flags: "r", encoding: null});
//Specify if stream Read error, return 404 error
stream.on("error", function() {
res.writeHead(404);
res.end("

404 Read Error});
//Pipeline connecting file stream and http return stream, used to return actual web content
stream.pipe(res);
}
else { //File does not exist
//Returns 404 error
res.writeHead(404, {"Content-Type": "text/html"});
res.end("

404 Not Found

");
}
});

2. Testing and running
1. Complete source code
The following 100 lines of JavaScript are All the source code to build such a simple web server:
Copy the code The code is as follows:

//------------------------------------------------ ------
//WebSvr.js
// A demo web server
//--------------------- --------------------------
//Start service startup timer
console.time('[WebSvr][Start] ');
//Request module
var libHttp = require('http'); //HTTP protocol module
var libUrl=require('url'); //URL parsing module
var libFs = require("fs"); //File system module
var libPath = require("path"); //Path analysis module
//Get the return content type string based on the path, used for http return Header
var funGetContentType=function(filePath){
var contentType="";
//Use the path parsing module to get the file extension
var ext=libPath.extname(filePath);
switch(ext){
case ".html":
contentType="text/html";
break;
case ".js":
contentType="text/javascript";
break;
case ".css":
contentType="text/css";
break;
case ".gif":
contentType="image/gif";
break;
case ".jpg":
contentType="image/jpeg";
break;
case ".png":
contentType="image/png";
break;
case ".ico":
contentType="image/icon";
break;
default:
contentType="application/octet-stream";
}
return contentType; //Return content type string
}
//Web server main function, parse the request, return Web content
var funWebSvr = function (req, res){
var reqUrl=req.url; //Get the requested url
//Output the requested path to the console
console.log(reqUrl);
//Use the url parsing module to get the path in the url Name
var pathName = libUrl.parse(reqUrl).pathname;
if (libPath.extname(pathName)=="") {
//If the path has no extension
pathName ="/ "; //Specify access directory
}
if (pathName.charAt(pathName.length-1)=="/"){
//If access directory
pathName ="index.html "; //Specify as the default web page
}
//Use the path parsing module to assemble the actual file path
var filePath = libPath.join("./WebRoot",pathName);
// Determine whether the file exists
libPath.exists(filePath,function(exists){
if(exists){//The file exists
//Write the content type in the return header
res.writeHead( 200, {"Content-Type": funGetContentType(filePath) });
//Create a read-only stream for return
var stream = libFs.createReadStream(filePath, {flags: "r", encoding: null} );
//Specify that if there is an error in reading the stream, a 404 error will be returned
stream.on("error", function() {
res.writeHead(404);
res.end("

404 Read Error

");
});
//Pipeline connecting the file stream and http return stream, used to return actual web content
stream.pipe(res) ;
}
else { //File does not exist
//Returns 404 error
res.writeHead(404, {"Content-Type": "text/html"});
res.end("

404 Not Found

");
}
});
}
//Create an http server
var webSvr=libHttp. createServer(funWebSvr);
//Specify server error event response
webSvr.on("error", function(error) {
console.log(error); //Output error information in the console
});
//Start listening to port 8124
webSvr.listen(8124, function(){
//Output service startup information to the console
console.log('[ WebSvr][Start] running at http://127.0.0.1:8124/');
//End the service start timer and output
console.timeEnd('[WebSvr][Start]');
});

2. Resource Directory

Since we want to establish a Web server, we need to create a WebRoot directory to store actual web pages and image resources. The directory name of "WebRoot" is used in the above source code to assemble the actual file path.

3. Run and test

Enter in the command line:

node.exe WebSvr.js

Our web server is running. At this time, it can be accessed through the browser. The operation effect is as follows:

Postscript

Using Node.js we can easily build a relatively independent web server. Its event-driven features avoid cumbersome thread protection, and its basic modules reduce the difficulty of development. The web server established in this chapter is just a simple sample, without too much consideration of modularity, security and other issues, but it can help you master some basic knowledge of Node.js development.
Author: Wang Feng www.otlive.cn

Statement:
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