express默认配置文件app.js
看完上面的,再回头看这app.js,就应该感觉没什么压力了,主要包含http的创建,基本路由,监听端口号
//引入模块 var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // 设置views路径和模板 app.set('views', path.join(__dirname, 'views')); //设置模板引擎 app.set('view engine', 'jade'); //app.use配置 app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public')));
//路由模块的使用 app.use('/', routes); app.use('/users', users); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handlers //路由和request的处理 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 error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app;
2024还活着,挺好的,向着星辰与深渊,加油,博客园不要倒下啊!