Settings for router in Expressjs server

Source: Internet
Author: User

Route setting method for Expressjs

First, the basic method:
App. METHOD (PATH, HANDLER)
The path and method of action (which can be HTTP get/put/delete, etc.) are mapped to a handler function handler

For example:
App.get ('/user ', function (req, res) {
Res.send (' Got a PUT request At/user ');
});

Second, use Express. Methods of Router

Defines a set of paths. For example:
/birds
/birds/about

1) First in the router file, define only relative paths/and/about
var express = require (' Express ');
var router = Express. Router ();

Define the home page route
Router.get ('/', function (req, res) {
Res.send (' Birds home page ');
});
Define the About route
Router.get ('/about ', function (req, res) {
Res.send (' About birds ');
});

Module.exports = router;

2) in the main file, define the base path/birds,
var birds = require ('./birds ');
App.use ('/birds ', birds);

Settings for router in 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.