generator-express-no-stress
Version:
Awesome APIs with ExpressJS and OpenAPI/Swagger: Features automatic request validation, interactive api doc, and more.
57 lines (51 loc) • 1.9 kB
JavaScript
import Express from 'express';
import * as path from 'path';
import * as bodyParser from 'body-parser';
import * as http from 'http';
import * as os from 'os';
import cookieParser from 'cookie-parser';
<% if (specification === 'openapi_3') { %>
import { OpenApiValidator } from 'express-openapi-validator';
import errorHandler from '../api/middlewares/error.handler';
<% } else { %>
import swaggerify from './swagger';
<% } %>
import l from './logger';
const app = new Express();
export default class ExpressServer {
constructor() {
const root = path.normalize(`${__dirname}/../..`);
app.set('appPath', `${root}client`);
app.use(bodyParser.json({ limit: process.env.REQUEST_LIMIT || '100kb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: process.env.REQUEST_LIMIT || '100kb' }));
app.use(bodyParser.text({ limit: process.env.REQUEST_LIMIT || '100kb'}));
app.use(cookieParser(process.env.SESSION_SECRET));
app.use(Express.static(`${root}/public`));
<% if (specification === 'openapi_3') { %>
const apiSpec = path.join(__dirname, 'api.yml');
const validateResponses = !!(
process.env.OPENAPI_ENABLE_RESPONSE_VALIDATION &&
process.env.OPENAPI_ENABLE_RESPONSE_VALIDATION.toLowerCase() === 'true'
);
app.use(process.env.OPENAPI_SPEC || '/spec', Express.static(apiSpec));
new OpenApiValidator({
apiSpec,
validateResponses,
}).install(app);
<% } %>
}
router(routes) {
<% if (specification === 'openapi_3') { %>
routes(app);
app.use(errorHandler);
<% } else { %>
swaggerify(app, routes);
<% } %>
return this;
}
listen(port = process.env.PORT) {
const welcome = p => () => l.info(`up and running in ${process.env.NODE_ENV || 'development'} @: ${os.hostname()} on port: ${p}}`);
http.createServer(app).listen(port, welcome(port));
return app;
}
}