//express.js routing Router learning
Router is express4.0 newly added functionality, it's like a mini-application that allows internal routing of applications written in a more convenient, more flexible.
//app.get and app.use difference
app object is generally used to represent express program, created by calling the exported top express express modules () method.
the difference:
The app.use (path, callback) callback router can be both an object may also be a function.
app.get / post (path, callback) callback is a function only.
understanding:
The app.get () is a schematic wording considered specific request (GET) of app.use, and examples are as follows:
var express = require("express");
was app = express;
app.get("/hello",(req,res)=>{
res.send("hello ");
})
And equivalent to the following
var express = require("express");
Ideas were Out = ();
var router = express.Router();
router.get("/",(req,res=>{
res.send(“hello”)
})) // Usually this portion routers in a separate file folder
app.use("/hello",router)
That is: the routing rules is app.use (path, router) defined object representative of a router created by express.Router, routing objects may be defined in a plurality of routing rules.
When we only one routing rule, a callback function can be directly connected with a short app.get; when a plurality of paths with a matching rule, app.use (path, router), router in a single configuration file which more rules.