app.js文件 搭建后自动生成的入口文件
var createError = require(‘http-errors‘);
var express = require(‘express‘);
var path = require(‘path‘);
var cookieParser = require(‘cookie-parser‘);
//cookie解析者解析cookie数据
//引入带有cookie-parser这种叫做中间件
//express中间件(当获取数据时对相应的数据流做预处理 )
var logger = require(‘morgan‘);
//logger代表着后台输出的一些数据
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‘, ‘ejs‘);//设置模板引擎ejs
app.use(logger(‘dev‘));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//use方法加载中间件
//post 把数据放在 body get把数据放在协议里
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‘) === ‘development‘ ? err : {};
// render the error page
res.status(err.status || 500);
res.render(‘error‘);
});
module.exports = app;
原文地址:https://www.cnblogs.com/fengch/p/8652152.html