require('./dto/init') var createError = require('http-errors') var express = require('express') var path = require('path') var cookieParser = require('cookie-parser') var lessMiddleware = require('less-middleware') var logger = require('morgan') const typeorm = require('typeorm') const event = require('./event') Object.defineProperty(express.response,'error',{ value:function (error,statusCode = 400,errObj = {}) { errObj.error = error this.status(statusCode).send(errObj) }, enumerable:false, writable:false, configurable:false }) Object.defineProperty(express.response,'list',{ value:function (data,resObj = {}) { resObj.list = data this.status(200).send(resObj) }, enumerable:false, writable:false, configurable:false }) Object.defineProperty(express.response,'detail',{ value:function (data,resObj = {}) { resObj.detail = data this.status(200).send(resObj) }, enumerable:false, writable:false, configurable:false }) Object.defineProperty(express.response,'success',{ value:function (msg,resObj = {}) { resObj.message = msg this.status(200).send(resObj) }, enumerable:false, writable:false, configurable:false }) /** * 连接数据库 */ typeorm.createConnection().then(()=>{ // 连接成功,通知bin/www,开始监听http event.emit('typeorm-connection') console.log('typeorm启动成功') }) var indexRouter = require('./routes/index') var usersRouter = require('./routes/users') var adminRouter = require('./routes/admin') var app = express() // view engine setup app.set('views', path.join(__dirname, 'views')) app.set('view engine', 'hjs') app.use(logger('dev')) app.use(express.json()) app.use(express.urlencoded({ extended: false })) app.use(cookieParser()) app.use(lessMiddleware(path.join(__dirname, 'public'))) app.use(express.static(path.join(__dirname, 'public'))) app.use('/', indexRouter) app.use('/users', usersRouter) app.use('/admin', adminRouter) // 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