| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- const express = require('express')
- const C = require('./controllers')
- const config = require('../config')
- const path = require('path')
- const chalk = require('chalk')
- const asyncHandler = require('express-async-handler')
- const bodyParser = require('body-parser')
- const routes = require('./routes')
- const app = express()
- app.use(bodyParser.json())
- routes(app)
- app.start = () => new Promise((resolve, reject) => {
- app.use(express.static('./public'))
- app.all('/api/*', (req, res) => {
- res.send(404)
- })
- app.get('*', (req, res) => {
- res.sendFile(path.join(process.cwd(), 'public/index.html'));
- })
- try {
- const listener = app.listen(config.server.port, function() {
- app.address = listener.address()
- console.log(`Server running at ${chalk.underline(chalk.blue(`http://localhost:${app.address.port}`))}`)
- })
- } catch (e) {
- reject(e)
- }
- })
- const asyncWrap = (method) => (function() {
- const args = Array.from(arguments)
- if (args[1]) args[1] = asyncHandler(args[1])
- return method.apply(app, args)
- })
- const asyncApp = Object.assign(Object.create(app), {
- get: asyncWrap(app.get),
- post: asyncWrap(app.post),
- put: asyncWrap(app.put),
- patch: asyncWrap(app.patch),
- delete: asyncWrap(app.delete)
- })
- module.exports = asyncApp
|