• express入口代码解释


    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;
    

      

  • 相关阅读:
    MySql入门知识(一)
    一起学Android之Handler
    一起学Android之Http访问
    一起学Android之Sqlite
    一起学Android之Storage
    iOS核心动画高级技巧-1
    iOS界面流畅技巧之微博 Demo 性能优化技巧
    IOS 企业版证书($299)In-House方式发布指南
    App Store生存指南
    企业证书发布APP
  • 原文地址:https://www.cnblogs.com/chenlw/p/10907072.html
Copyright © 2020-2023  润新知