In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use node.js to build a simple web server". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use node.js to build a simple web server" can help you solve the problem.
Prepare for
First of all, you need to install nodejs, which can be downloaded from the official website. I currently have v0.12 version installed locally.
After the installation is complete, you can test the success of the installation from the command line. Enter: node-v, and the current installation node version number should be displayed.
The modules used in this article are nodejs core modules and do not need to be downloaded from the outside. If necessary, you can install them with the following command: npm install xxx.
Start
Next, create a new js file, which can be named server.js, with the following code:
Var http = require ('http'); var url = require (' url'); var path = require ('path'); var fs = require (' fs'); var dir, arg = process.argv [2] | |'' / / the third parameter on the command line, which is used to receive the directory, can be empty, relative to the directory name of the current server.js file / / for example, using the command node server debug, which means that the debug folder is at the same level as the server.js file / / and you want to start the web service http.createServer (function (req, res) {var pathname = _ dirname + url.parse (req.url) .pathname; dir = dir? Dir: pathname; / / remember dir (directory) pathname = dir? Pathname.replace (dir, dir + arg +'/'): pathname; / / replace the file static path if (path.extname (pathname) = "") {pathname + = "/";} if (pathname.charAt (pathname.length-1) = = "/") {pathname + = "index.html" / / entry file, where the default is index.html} fs.exists (pathname, function (exists) {if (exists) {switch (path.extname (pathname)) {case ".html": res.writeHead (200,{ "Content-Type": "text/html"}); break; case ".js": res.writeHead (200,{ "Content-Type": "text/javascript"}); break Case ".css": res.writeHead (200,{ "Content-Type": "text/css"}); break; case ".gif": res.writeHead (200,{ "Content-Type": "image/gif"}); break; case ".jpg": res.writeHead (200,{ "Content-Type": "image/jpeg"}); break; case ".png": res.writeHead (200,{ "Content-Type": "image/png"}); break Default: res.writeHead (200,{ "Content-Type": "application/octet-stream"});} / / res can add information for simple interaction such as modifying point header information or modifying the returned resource data fs.readFile (pathname, function (err, data) {res.end (data);});} else {res.writeHead (404, {"Content-Type": "text/html"}); res.end ("404 Not Found") });}) .server (8085, "127.0.0.5"); / / server port console.log ("server running at http://127.0.0.5:8085/");")
Start
When the node installation is complete and the above server.js file is also created. Put it with the folder you want to access, either on the same level or directly below. For example, if you want to access the d:\ test\ debug folder.
You can first put the current file on the same layer or directly under it, 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: `debug` (same layer), or `node server` (sublayer)
At this point, the prompt `server running at http://127.0.0.5:8085/`, indicates that the service has been started successfully
Finally, open the browser and enter: `127.0.0.5: 8085` to access this resource.
This is the end of the introduction to "how to use node.js to build a simple web server". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.