Nodejs+express Development Application-InitializationGlobal Installation Express
$ NPM Install Express-g
If you've already installed express before, you won't have to do this step.
Initializing the App
Initialize a web app that supports session by using the Ejs template named MyApp:
$ express--sessions --ejs MyApp
After execution, follow the prompts to continue
CD MYAPPNPM Install
[Note]:express–help View all the Express commands
Set the key for a cookie
Within the app.js of the app root, modify the key your secret here
to a custom keymyappsecret
App.use (Express.cookieparser (' Myappsecret '));
Configure Routing
The default route for Express is to introduce each routing configuration directly in the App.js.
However, there are many inconveniences in this way and it is recommended that each route be configured separately.
The configuration method is
Each routing file is used as a module, and multiple route files can be combined into one package, and the app is passed as a parameter in each routing module.
Simply introduce a route file in App.js and pass the app to each routing module
/*app.js*/var routes = require ('./routes ');/*other code*/routes (APP);
A main routing module is set up in the routing module Index.js,
require(‘./routes‘)
Which is the introduction of routes/index.js
modules
/* * Routes/index.js * account is another sub-route that can be a package, or a module, module, or package that follows the rules of node's module and package. */var account = require ("./account"); module.exports = function (APP) { /* Other Routing rules * /App.all ("/", function (req, RES) { /* processing request * /}) account (app); /* You can define additional routes */};
Account as a module:
/*account as a module */var account=require (".. /controllers "). Account; Introduction of relevant Controllermodule.exports=function (app) { app.get ('/account/adminlogin ', account.adminlogin); App.post ('/account/adminlogin ', account.adminloginpost);};
Account as a package
/** * Account as a routing package * Routes/account/index.js */var user=require ("./user"); Require routes/account/user.jsmodule.exports=function (APP) { user (app);}
Nodejs+express Development Application-Initialization