You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.1 KiB
84 lines
2.1 KiB
import express from 'express'
|
|
import compression from 'compression'
|
|
import morgan from 'morgan'
|
|
import cors from 'cors'
|
|
import http from 'http'
|
|
import path from 'path'
|
|
import yaml from 'yamljs'
|
|
import * as api from './api'
|
|
import { accessTokenAuth } from './security'
|
|
import { connector, summarise } from 'swagger-routes-express'
|
|
|
|
const app = express()
|
|
const port = 9528
|
|
|
|
// Compression
|
|
app.use(compression())
|
|
// Logger
|
|
app.use(morgan('dev'))
|
|
// Enable CORS
|
|
app.use(cors())
|
|
// POST, PUT, DELETE body parser
|
|
app.use(express.json({ limit: '20mb' }))
|
|
app.use(express.urlencoded({
|
|
limit: '20mb',
|
|
extended: false
|
|
}))
|
|
// No cache
|
|
app.use((req, res, next) => {
|
|
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate')
|
|
res.header('Pragma', 'no-cache')
|
|
res.header('Expires', '-1')
|
|
next()
|
|
})
|
|
|
|
// Read and swagger config file
|
|
const apiDefinition = yaml.load(path.resolve(__dirname, 'swagger.yml'))
|
|
// Create mock functions based on swaggerConfig
|
|
const options = {
|
|
security: {
|
|
AccessTokenAuth: accessTokenAuth
|
|
}
|
|
}
|
|
const connectSwagger = connector(api, apiDefinition, options)
|
|
connectSwagger(app)
|
|
// Print swagger router api summary
|
|
const apiSummary = summarise(apiDefinition)
|
|
console.log(apiSummary)
|
|
|
|
// Catch 404 error
|
|
app.use((req, res) => {
|
|
const err = new Error('Not Found')
|
|
res.status(404).json({
|
|
message: err.message,
|
|
error: err
|
|
})
|
|
})
|
|
|
|
// Create HTTP server.
|
|
const server = http.createServer(app)
|
|
|
|
// Listen on provided port, on all network interfaces.
|
|
server.listen(port)
|
|
server.on('error', onError)
|
|
console.log('Mock server started on port ' + port + '!')
|
|
|
|
// Event listener for HTTP server "error" event.
|
|
function onError(error: any) {
|
|
if (error.syscall !== 'listen') {
|
|
throw error
|
|
}
|
|
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case 'EACCES':
|
|
console.error('Express ERROR (app) : %s requires elevated privileges', bind)
|
|
process.exit(1)
|
|
case 'EADDRINUSE':
|
|
console.error('Express ERROR (app) : %s is already in use', bind)
|
|
process.exit(1)
|
|
default:
|
|
throw error
|
|
}
|
|
}
|