node. JS Learning Notes under Windows (7) Detailed configuration instructions for the app.js of---Express

Source: Internet
Author: User

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

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.