Path settings for Node EXPRESSJS server

Source: Internet
Author: User

First, the path of the dynamic page:

App. METHOD (PATH, HANDLER)
Where:

* App is an instance of Express.
* method is an HTTP request method. Get,post, Put,delete,
* Path is a path on the server.
* HANDLER is the function executed if the route is matched.

Cases:
1) App.get ("/", F1);
2) App.post ("/", F2);

Map the get/post operation of the URL path directly to a function with response,

F1: Can be a file in this module, or a file in another module, or it can be a router module

App.use ("/", separate router module)

Second, the static file routing:

App.use (Express.static ('./public '));

* * You can also define a virtual path

App.use ('/virtualpathname ', express.static ('./public '));

The directory must be based on the root directory of the Web site, regardless of the current JS file directory

var path = require (' path ');
App.use (Express.static (Path.join (__dirname, ' public '));


Example: Access to images/kitten.jpg:
App.use (express.static (' public '));
Http://localhost/images/kitten.jpg

If the virtual path is used:
App.use ('/static ', express.static (' public '));
Http://localhost/static/images/kitten.jpg

Http://expressjs.com/en/starter/static-files.html

Http://expressjs.com/en/guide/routing.html

Http://expressjs.com/en/4x/api.html#router

Path settings for Node EXPRESSJS server

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.