Express is the official recommended framework for node. js.
Installation
NPM Install Express-g
"-G" in the command indicates global
Because the new version (4.x.x) Express commands are centralized into another plug-in, the Express-generator is also installed.
NPM Install Express-generator-g
version Check
Express-v
Build an Express project
Express Project-name
You can see the installed directory in the computer tray
Bin: Internal Command folder public: Put pictures, scripts, styles of folders (images, JS, CSS) Routes: Routing Folder views: Page folder app.js: Project portal, Equivalent to Index.phppackage.json in PHP: Project dependency Configuration and developer information
Open a app.js file in a project
Introduction module var Express = require (' Express '), var path = require (' path '), var favicon = require (' Serve-favicon '); var logger = requ IRE (' Morgan '); var cookieparser = require (' Cookie-parser '); var bodyparser = require (' Body-parser '); var routes = require ( './routes/index '); var users = require ('./routes/users '); var app = Express ();//view, template settings App.set (' Views ', Path.join (__ DirName, ' views '); App.set (' View engine ', ' jade ');//Uncomment after placing your Favicon In/public//app.use (Favicon ( Path.join (__dirname, ' public ', ' Favicon.ico ')); App.use (Logger (' dev ')); App.use (Bodyparser.json ()); App.use ( Bodyparser.urlencoded ({extended:false})); App.use (Cookieparser ()); App.use (Express.static (Path.join, ' Public ')); App.use ('/', routes); App.use ('/users ', users);//capture, handle 404 error App.use (function (req, res, next) {var err = new Er Ror (' not Found '); Err.status = 404; Next (err);}); /Development Environment error handling if (App.get (' env ') = = = ' Development ') {App.use (err, req, res, next) {Res.status (Err.status | | 500 ); Res.renDer (' Error ', {message:err.message, error:err}); });} Error handling in the context of the product app.use (err, req, res, next) {Res.status (Err.status | | 500); Res.render (' error ', {message:err.message, error: {}}); /External Output interface module.exports = app;
Open Package.json File
{ "name": "Project-name", "version": "0.0.0", " private": True, "scripts": { "start": "Node./bin /www " }, " dependencies ": { " Body-parser ":" ~1.13.2 ", " Cookie-parser ":" ~1.3.5 ", " Debug ":" ~ 2.2.0 ", " Express ":" ~4.13.1 ", " Jade ":" ~1.11.0 ", " Morgan ":" ~1.6.1 ", " Serve-favicon ":" ~2.3.0 " }}
Installing some of the development-assisted modules
Supervisor Automatically restarts each time the code is modified
Installing NPM install supervisor-g//calling supervisor App.js
Forever Process Management allows node service to not stop
Installing NPM install forever-g//call Forever start App.js
Meet Node.js:express (i)