var createError = require('http-errors');//404检测错误页
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');//解析cookie
var logger = require('morgan');//生成日志

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup 注册前端模板视图
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));//注册日志
app.use(express.json());//post请求参数JSON格式 req.body获取
app.use(express.urlencoded({ extended: false }));//类似表单提交参数获取
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));//注册静态文件

//注册路由
app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler 程序出问题时的提示
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'dev' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

  

posted on 2019-05-22 17:03  chenlw101  阅读(943)  评论(0编辑  收藏  举报