Nodejs build a local Web test server and solve the problem of accessing static resources!

Source: Internet
Author: User
Tags node server

Open the HTML file directly, is opened in file:///way, this way often encounter cross-domain problems, so we usually build a simple local server, to run the test page.

First, build a static server

1. Using Express module

Create a JS file, name the server, the content code is as follows:

1 varExpress = require (' Express ');2 varApp =Express ();3 varPath = require (' path ');4 5 //specifying a static resource access directory6App.use (express.static (Require (' path '). Join (__dirname, ' public ')));7 //App.use (express.static (Require (' path '). Join (__dirname, ' views ')); If you have a folder to store resources, if there is an error, then use a few more times .8 //set the views variable, which means the directory where the view is stored9App.set (' views ', (__dirname + "/public")));Ten //app.set (' views ', __dirname); One //Modify the template file suffix named HTML AApp.set (' View engine ', ' HTML ' ); - //running the Ejs module -App.engine ('. html ', require (' Ejs ')). __express); the  -App.get ("/",function(req, res) { -Res.render (' Index '); - }); +  - varServer = App.listen (1336, "127.0.0.1",function(){ +     varHost =server.address (). Address; A     varPort =server.address (). Port; atConsole.log ("Server running at http://%s:%s", host, port) -});

The file structure is as follows:

Run the word as long as it executes: node server.js yes.

Then in the browser input http://127.0.0.1:1336/ to access the files in the project folder

2. Using the Connect module

Create a JS file, nameserver2 ,内容代码如下:

var connect = require ("Connect"var servestatic = require ("serve-static"var app =//  App.use (servestatic ("c:\\xxx\\xxx\\xxx\\ project Folder")) App.use (Servestatic (" Public ")); App.listen (1337); Console.log (' Server running at http://127.0.0.1:1337/');

Run the words as long as the execution: node server2.js Yes,

Then the browser input http://127.0.0.1:1337/ to access the files in the project folder. (If the index.html file can be omitted, this file is loaded by default);

3. Using the HTTP Module

Create a JS file, nameserver3 ,内容代码如下:

1 varFinalhandler = require (' Finalhandler '))2 varHTTP = require (' http ')3 varServestatic = require (' serve-static '))4 5 //Serve up Public/ftp folder6 varServe = servestatic (' public ', {' index ': [' index.html ', ' index.htm ']})7 8 //Create Server9 varServer = Http.createserver (functiononrequest (req, res) {Ten Serve (req, res, Finalhandler (req, res)) One }) A  - //Listen -Server.listen (1338); theConsole.log (' Server running at http://127.0.0.1:1338/');

Run the words as long as the execution: node server3.js Yes,

Then the browser input http://127.0.0.1:1338/ to access the files in the project folder.

Note: The total file directory is as follows:

Source: Https://github.com/arvin0/nodejs-example/tree/master/web-static-test-server

Ii. resolving access to static resources

Mainly uses two modules

1. General-Purpose serve-static module

Detail Document: Https://github.com/expressjs/serve-static

2.express exclusive App.use (express.static (Require (' path '). Join (__dirname, ' Public ' )); Method

Detailed documentation: http://expressjs.com/en/4x/api.html, and then ctrl+f search express.static , you can find the corresponding instructions.

Nodejs build a local Web test server and solve the problem of accessing static resources!

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.