3 minutes to quickly build a nodejs local server to run the test html/js, nodejs local server

Source: Internet
Author: User

3 minutes to quickly build a nodejs local server to run the test html/js, nodejs local server

All the front-end users know that it is quite troublesome to build an environment on their own computers for the company's projects.

  1. First of all: Generally, personal computers do not have good performance in the company's configuration,
  2. Second, setting up a company project involves many cache environments, resource environment packages, databases, and other restrictions on its own computer. This is enough for a day (except for environments familiar with the company)

In my own company, this is the case. As a front-end engineer,

Currently, the front-end is based on polymer. The problem encountered recently is that you do not know if there are any problems with the pages you have written. However, you need to successfully start tomcat for the items, because these pages cannot be opened directly, will package resource parsing error,

So I was wondering, how can I deploy servers that don't rely on tomcat on front-end pages?

So Baidu saw nodejs and nginx and started his practice with curiosity about the front-end server. He also consulted some students one night and got some inspiration.

The procedure is as follows:

1. Download the installation package from the official node. js Website: http://nodejs.cn/after the installation is complete, the environment is automatically reflected in the system. If you do not need to configure environment changes, open the console.
Enter the command: node

If no error is reported, the operation is successful.

2. Create the following two js files under 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); // set your own file name here; 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 (introduce the corresponding file)

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 files are created successfully, open http. js, find the path settings in it, and change my current webapp to your own name. Project Name

Friendship tips: (the premise is that it is in the same directory as js. If the test project is in another file, you may need to set the absolute path)
The following is the file corresponding to my directory. The three arrows are essential and do not need to be set.

 

3. test whether the server is successfully started on the console

See:

Open the project directory and enter node http. js.

Then, the system prompts that the port is successfully started,

Enter the address of the file you want to test. For example, http: // localhost: 3000/index.html

All deployment has been completed!

Note: you must add your own file path after the port number to ensure the operation is successful. Otherwise, an error will be reported because it is relatively simple to write in http. js and is not directly mapped at the moment.
Http: // localhost: 3000

If an error occurs, rewrite and start the service.

That is, Node. js

If you are interested, rewrite node. js to improve it.

Last said: I saw some great guys on my blog and Baidu, but their tutorials are not clear enough (I cannot keep up with their ideas ), so thanks again to these blogs.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.