Home >Backend Development >PHP Tutorial >How to build a server using node.js
Everyone who works on the front end knows that it is quite troublesome to set up the environment for the company's projects on your own computer. This article mainly shares with you the node.js server setting method, hoping to Help everyone.
First of all: Generally, personal computers do not have as good performance as the company's configuration.
Secondly: Building a company project on your own computer involves a lot of caching environment and resources. The limitations of environment packages, databases, etc., add up to enough for a day (except for environments built by familiar companies)
This is the case for my own company. As a front-end engineer,
The front-end is currently based on polymer - the problem I encountered recently is that I don't know if there are any problems with the pages I wrote, but I have to successfully start the tomcat of the project to view them, because these pages cannot be opened directly and will contain resource parsing errors.
So I was thinking, how can I make the front-end page not rely on the painful deployment server like tomcat?
So Baidu saw nodejs and nginx, and started its own practice with curiosity about the front-end server. I also consulted some classmates one night, and then got some inspiration
The steps are as follows
1. Go to the nodejs official website Download the installation package http://nodejs.cn/ After the installation is completed, the environment is automatically mapped to the system. There is no need to configure the environment variables yourself, and then open the console
Enter the command: node
No error means the operation is successful
2. Then create the following two js files in the same directory as the test directory
http.js (server script configuration file)
var PORT = 3000;//var http = require('http');var url=require('url');var fs=require('fs');var mine=require('./mine').types;//var path=require('path');var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; var realPath = path.join("webapp", pathname); //这里设置自己的文件名称; var ext = path.extname(realPath); ext = ext ? ext.slice(1) : 'unknown'; fs.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("This request URL " + pathname + " was not found on this server."); response.end(); } else { fs.readFile(realPath, "binary", function (err, file) { if (err) { response.writeHead(500, { 'Content-Type': 'text/plain' }); response.end(err); } else { var contentType = mine[ext] || "text/plain"; response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } }); }); server.listen(PORT); console.log("Server runing at port: " + PORT + ".");
mine.js (introducing the corresponding files)
exports.types = { "css": "text/css", "gif": "image/gif", "html": "text/html", "ico": "image/x-icon", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "text/javascript", "json": "application/json", "pdf": "application/pdf", "png": "image/png", "svg": "image/svg+xml", "swf": "application/x-shockwave-flash", "tiff": "image/tiff", "txt": "text/plain", "wav": "audio/x-wav", "wma": "audio/x-ms-wma", "wmv": "video/x-ms-wmv", "xml": "text/xml"};
After the above two js are successfully created, open http.js, then find the path setting inside, and change my current webapp to your own name project name
Friendship tips: (provided that it is with js Directory at the same level. If the test project is in other files, the path may need to be set as an absolute path_I have not tried this)
The following is the file corresponding to my directory, the three arrows are necessary and no other settings are required
3. The console starts the server test to see if it is successful
See the picture below:
Open the directory where the project is located and enter node http.js
Then it prompts that the port is started successfully,
Then just enter the address of the file you want to test directly. For example, my
http://localhost:3000/index.html
is now fully deployed!
Note here: You must add your own file path after the port number to run successfully. Otherwise, an error will be reported because the writing in http.js is relatively simple and has not been directly mapped for the time being
http://localhost:3000
If an error occurs, please rewrite the startup service
That is: node.js
Of course, if you are interested, you can rewrite node.js and improve it.
Finally: These two js are written by some big guys that I saw on blogs and Baidu, but they The tutorials written by others are not clear enough (I am not good enough to keep up with their ideas),
So thank you again to these bloggers,
Then try:
Run the test file on nginx,
Build a nodejs+mangodb test project and migrate the angularjs tree to polymer
The angular tree was written by the previous front-end expert, and I never quite understood it. I didn’t go to Baidu to read what other experts wrote. ng tree (maybe there are many more powerful ones)
Related recommendations:
How to build a server with nodejs
The above is the detailed content of How to build a server using node.js. For more information, please follow other related articles on the PHP Chinese website!