57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { isTestInstance } from './server/helpers/core-utils'
|
|
if (isTestInstance()) {
|
|
require('source-map-support').install()
|
|
}
|
|
|
|
import * as bodyParser from 'body-parser'
|
|
import * as express from 'express'
|
|
import * as cors from 'cors'
|
|
import * as morgan from 'morgan'
|
|
import { apiRouter } from './server/controllers/api'
|
|
import { logger } from './server/helpers/logger'
|
|
import { API_VERSION, CONFIG } from './server/initializers/constants'
|
|
|
|
const app = express()
|
|
|
|
app.use(morgan('combined', {
|
|
stream: { write: logger.info.bind(logger) }
|
|
}))
|
|
|
|
app.use(bodyParser.json({
|
|
type: [ 'application/json', 'application/*+json' ],
|
|
limit: '500kb'
|
|
}))
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
// ----------- Views, routes and static files -----------
|
|
|
|
app.use(cors())
|
|
|
|
const apiRoute = '/api/' + API_VERSION
|
|
app.use(apiRoute, apiRouter)
|
|
|
|
// ----------- Errors -----------
|
|
|
|
// Catch 404 and forward to error handler
|
|
app.use(function (req, res, next) {
|
|
const err = new Error('Not Found')
|
|
err['status'] = 404
|
|
next(err)
|
|
})
|
|
|
|
app.use(function (err, req, res, next) {
|
|
let error = 'Unknown error.'
|
|
if (err) {
|
|
error = err.stack || err.message || err
|
|
}
|
|
|
|
logger.error('Error in controller.', { error })
|
|
return res.status(err.status || 500).end()
|
|
})
|
|
|
|
// ----------- Run -----------
|
|
|
|
app.listen(CONFIG.LISTEN.PORT, () => {
|
|
logger.info('Server listening on port %d', CONFIG.LISTEN.PORT)
|
|
})
|