Router.get ("/", Function (req, res) {
Res.render ("index", {"title": "Express"});
});
This code means that when accessing the home page, the Ejs template engine is called to render the Index.ejs template file (replacing all the title variables with String Express), generating a static page and displaying it in the browser.
We have to make some changes, the above code to achieve the function of the route, we can of course do not routes/index.js files, the implementation of the routing function of the code into the app.js, we certainly do not routes/ Index.js file, the implementation of the code to implement the routing function in the app.js, but over time App.js will become bloated and difficult to maintain, which also violates the code of modular thinking, so we put the implementation of the Code of the routing function in the routes/index.js. The official notation is to implement a simple routing assignment in App.js, and then to find the corresponding route function in the Index.js, and finally implement the routing function. We might as well put the routing controller and the implementation of the route ENG male functions are put into the index.js, app.js only a total routing interface.
Finally, the app.js revision was changed to
var express require ("express");
var path = require ("path");
var favicon = require ("Server-favicon");
var logger = require ("Morgan");
var cookieparser = require ("Cookie-parser");
var bodyparser = require ("Body-parser");
var routes = require ("./routes/index");
var app = Express ();
App.set ("Port", Process.env.PORT | | 3000);
App.set ("View", Path.join (_dirname, "views"));
App.set ("View Engine", "Ejs");
App.use (Favicon (__dirname + "/public/favicon.ico"));
App.use (Logger ("dev"));
App.use (Bodyparser.json ());
App.use (bodyparser.urlencoded ({extended:true}));
App.use (Cookieparser ());
App.use (Express.static (Path.join (__dirname, "public"));
Routes (app);
App.listen (App.get ("port"), function () {
Console.log ("Express Server listening on port" + app.get ("port"));
});
Modify the Index.js as follows:
Module.exports = function (APP) {
App.get ("/", Function (req, res) {
Res.render ("index", {title: "Express"});
});
}
Routing rules:
Express encapsulates a variety of HTTP request methods, we mainly only use Get and post two, namely App.get () and App.post ().
The first parameters of App.get () and App.post () are the requested path, the second parameter is the callback function that handles the request, and the callback function has two parameters, req and res respectively, representing the request information and the response information.
Path requests and corresponding fetch paths are available in the following ways:
Req.query // GET /search?q=tobi+ferretreq.query.q// = "Tobi Ferret"// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=conversereq.query.order // = "Blue"req.query.shoe.type// = "Converse"
Req.body // POST user[name]=tobi&user[email][email protected]req.body.user.name// = > "Tobi"req.body.user.email// = "[email protected]"// post{"name": "Tobi"}req.body.name// = "Tobi"
Req.params // get /user/tjreq.params.name// = "TJ"//get /file/ Javascripts/jquery.jsreq.params[0]// = "Javascripts/jquery.js"
How Experss Routing Works