varExpress = require (' Express ');varPath = require (' path ');varFavicon = require (' Serve-favicon '));varLogger = require (' Morgan ');varCookieparser = require (' Cookie-parser '));varBodyparser = require (' Body-parser '));varRoutes = require ('./routes/index '));varUsers = require ('./routes/users '));varApp =Express ();//set the views path and template
__dirname is the global variable within node. js, which is the path to the execution of JS, and the __dirname is the currently executing JS file name. So, app.set (' views ', __dirname + '/views ') is a file//clip that sets views.
App.set (' Views ', Path.join (__dirname), ' views '));
Sets the render engine used by the express.js. In addition to Jade, Express.js supports JS templates such as Ejs (embedded JavaScript), Haml, Coffescript, and jquery template. App.set (' View engine ', ' Jade ');//uncomment after placing your favicon in/public//App.use (Favicon (__dirname + '/public/favicon.ico '));
App.use Configuration
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);//catch 404 and forward to error handlerApp.use (function(req, res, next) {varErr =NewError (' Not Found '); Err.status= 404; Next (err);});//Error Handlers//Development Error Handler//Would print StackTraceif(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 Error Handler//no stacktraces leaked to userApp.use (function(Err, req, res, next) {Res.status (Err.status|| 500); Res.render (' Error '{message:err.message, error: {}}); Module.exports= app;
node. JS Learning Notes under Windows (7) Detailed configuration instructions for the app.js of---Express