Preface
Building a web server using Nodejs is a relatively comprehensive introductory tutorial. Because to complete a simple web server, you need to learn several more important modules in Nodejs, such as: http protocol module, file system, url parsing module, path parsing module, and 301 redirection problem. Let's briefly talk about how to build a simple web server.
If you want to access local resources on the browser side without using a web server, you can use the firefox browser, which can start a small web server by itself.
In order for people who are new to node to understand it, I will try to simplify the code in this article.
Prepare
First, installation is requirednodejs, this can be downloaded on the official website. Currently, I installed the v0.12 version locally.
After the installation is completed, you can test whether the installation is successful through the command line and enter:node -v
, the current installed node version number should be displayed.
The modules used in this article are all nodejs core modules and do not need to be downloaded from the outside. If necessary, you can use the following command to install:npm install xxx
。
start
Next, create a new js file, which can be named as follows:
var http = require('http'); var url = require('url'); var path = require('path'); var fs = require('fs'); var dir, arg = [2] || ''; // The third parameter of the command line is used to receive the directory, which can be empty, relative to the directory name of the current file // For example, using the command node server debug, which means that the debug folder and file are at the same level // And you want to start the web service with the debug folder (function (req, res) { var pathname = __dirname + ().pathname; dir = dir ? dir : pathname; // Remember dir(directory) pathname = dir ? (dir, dir + arg + '/') : pathname; // Replace the static path of the file if ((pathname) == "") { pathname += "/"; } if (( - 1) == "/") { pathname += ""; // Entry file, default here } (pathname, function (exists) { if (exists) { switch ((pathname)) { case ".html": (200, {"Content-Type": "text/html"}); break; case ".js": (200, {"Content-Type": "text/javascript"}); break; case ".css": (200, {"Content-Type": "text/css"}); break; case ".gif": (200, {"Content-Type": "image/gif"}); break; case ".jpg": (200, {"Content-Type": "image/jpeg"}); break; case ".png": (200, {"Content-Type": "image/png"}); break; default: (200, {"Content-Type": "application/octet-stream"}); } // Res can add information by itself for simple interaction, such as modifying the header information or modifying the returned resource data (pathname, function (err, data) { (data); }); } else { (404, {"Content-Type": "text/html"}); ("<h1>404 Not Found</h1>"); } }); }).listen(8085, "127.0.0.5"); // Server port ("server running at http://127.0.0.5:8085/");
start up
After the node installation is completed and the above files are newly created. Put it together with the folder you want to access, you can place it on the same layer or directly on the lower layer. For example, if you want to access the d:\test\debug folder.
You can first put the current file into the same layer or directly, and then enter the following command to start the web service:
- First open `cmd` and enter the directory where the server file is located, such as the `test` directory;
- Then enter:`
node server debug
`(Same level), or`node server
`(sub-layer), - It will be prompted at this time`
server running at http://127.0.0.5:8085/
`, indicating that the service is started successfully; - Finally, open the browser and go to: `127.0.0.5:8085` to access this resource.
at last
A brief explanation of the above code.
First of all, the uppermost require indicates which modules you need to use, please refer to them first;
arg represents the third parameter of the input command line, which is manually intercepted;
The createServer method represents the creation of an http service, taking a function as a parameter, and an anonymous function is passed into the code of this article;
- req represents the http request object, which carries relevant information from the client's http request this time, such as request method, request query parameters, request header header header information, etc.;
- res means http response (return) object, used to return request resources to the client. Information can be added manually, such as the returned data, the returned header information, etc., the returned code, etc.;
- fs represents the file resource object. You can access the API of the nodejs official website;
- path, represents the resource path object, you can access the API of the official website of nodejs.
listen means the created service listening. Once this port is accessed, it will enter the previous anonymous function callback and return the resource to the client.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.