Express is the NODEJS standard Web development Framework
Reference http://blog.fens.me/nodejs-express4/
Install the Project Builder
Install Npminstall-g Express First
Re-install NPM install-gexpress-generator #全局安装-G
Check the installation results express-v# Check the version of Express
Uninstall (use if needed)
NPM Uninstall-g Express
Reference
http://blog.csdn.net/JBBOY/article/details/25038299
Http://jingyan.baidu.com/album/922554468a3466851648f419.html?picindex=1
Create a project
Express-enodejs-demo
Enter the project directory, download the dependent library, build the project.
CD Nodejs-demo && NPM Install
Start Project
NPM start
Effects such as
Directory Structure Description
nodejs-demo/
├──app.js Application Core configuration file
├──bin script files for startup projects
├──node_modules Project Dependency Library
├──package.json Project Dependency Configuration and developer information
├──public static file (css,js,img)
├──routes Routing file (C,controller in MVC)
└──views page file (ejs template)
Package.json
Where the Scripts property is used to define the operation command, it is very convenient to add the start command, such as the default start, and the NPM start to represent the node./bin/www command.
App.js file
Load Dependent Library, originally this class library is encapsulated in Connect, now need to note load separately
Varexpress = require (' Express ');
Varpath = require (' path ');
Varfavicon = require (' Serve-favicon ');
Varlogger = require (' Morgan ');
Varcookieparser = require (' Cookie-parser ');
Varbodyparser = require (' Body-parser ');
Load Routing Control
Varroutes = require ('./routes/index ');
Varusers = require ('./routes/users ');
Create a Project instance
Varapp = Express ();
Define the Ejs template engine and template file location, or you can use Jade or another model engine
App.set (' Views ', Path.join (__dirname, ' views '));
App.set (' Viewengine ', ' Ejs ');
Defining icon Icons
App.use (Favicon (__dirname+ '/public/favicon.ico '));
Defining logs and output levels
App.use (Logger (' dev '));
Defining Data parsers
App.use (Bodyparser.json ());
App.use (bodyparser.urlencoded ({extended:false}));
Defining a Cookie Parser
App.use (Cookieparser ());
Define a static file directory
App.use (Express.static (Path.join (__dirname, ' public '));
Matching paths and routes
App.use ('/', routes);
App.use ('/users ', users);
404 Error handling
App.use (function (Req,res, next) {
var err = new Error (' not Found ');
Err.status = 404;
Next (ERR);
});
Development environment, 500 error handling and error stack tracing
if (App.get (' env ') = = = ' Development ') {
App.use (function (err, req, res, next) {
Res.status (Err.status | | 500);
Res.render (' Error ', {
Message:err.message,
Error:err
});
});
}
Production environment, 500 error handling
App.use (function (Err,req, res, next) {
Res.status (Err.status | | 500);
Res.render (' Error ', {
Message:err.message,
Error: {}
});
});
Output Model App
module.exports= app;
www file
The www file is also a node script that separates configuration and startup programs.
View the./bin/www file.
#!/usr/bin/envnode
/**
* Dependent loading
*/
Varapp = require ('.. /app ');
Vardebug = require (' Debug ') (' nodejs-demo:server ');
Varhttp = require (' http ');
/**
* Define the boot port
*/
Varport = Normalizeport (Process.env.PORT | | ' 3000 ');
App.set (' Port ', port);
/**
* Create an HTTP server instance
*/
Varserver = Http.createserver (APP);
/**
* Start the Network Service listening port
*/
Server.listen (port);
Server.on (' Error ', onError);
Server.on (' Listening ', onlistening);
/**
* Port Normalization function
*/
Functionnormalizeport (val) {
var port = parseint (val, 10);
if (IsNaN (port)) {
return Val;
}
if (port >= 0) {
return port;
}
return false;
}
/**
* HTTP Exception event handler function
*/
Functiononerror (Error) {
if (Error.syscall!== ' listen ') {
Throw error;
}
var bind = typeof Port = = = ' String '
? ' Pipe ' + port
: ' Port ' + port
Handle specific Listen errors withfriendly messages
Switch (error.code) {
Case ' eacces ':
Console.error (bind + ' requires elevatedprivileges ');
Process.exit (1);
Break
Case ' Eaddrinuse ':
Console.error (Bind + ' is already inuse ');
Process.exit (1);
Break
Default
Throw error;
}
}
/**
* Event Binding function
*/
Functiononlistening () {
var addr = server.address ();
var bind = typeof Addr = = = ' String '
? ' Pipe ' + addr
: ' Port ' + addr.port;
Debug (' Listening on ' + bind);
}
Reference
http://blog.fens.me/nodejs-express4/
Node. JS Learning--(4)--express4.x framework