Nodejs+express Development Application-Initialization

Source: Internet
Author: User

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

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.