create-express-typescript-application
Version:
Create a new lightweight Express application with TypeScript
68 lines (56 loc) • 2.36 kB
text/typescript
import express, { Express } from 'express';
import { PrismaClient } from '@prisma/client';
import morgan from 'morgan';
import helmet from 'helmet';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import { ValidateError } from 'tsoa';
import { RegisterRoutes } from '../tsoa/routes';
const app: Express = express();
/************************************************************************************
* Basic Express Middlewares
***********************************************************************************/
app.set('json spaces', 4);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Handle logs in console during development
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
app.use(cors());
}
// Handle security and origin in production
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'production') {
app.use(helmet());
}
/************************************************************************************
* Register all routes
***********************************************************************************/
RegisterRoutes(app);
app.use("/docs", swaggerUi.serve, async (req: express.Request, res: express.Response) => {
return res.send(swaggerUi.generateHTML(await import("../tsoa/swagger.json")));
});
/************************************************************************************
* Express Error Handling
***********************************************************************************/
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
if (err instanceof ValidateError) {
console.error(`Caught Validation Error for ${req.path}:`, err.fields);
return res.status(422).json({
message: "Validation Failed",
details: err?.fields,
});
}
if (err instanceof Error) {
return res.status(500).json({
errorName: err.name,
message: err.message,
stack: err.stack || 'no stack defined'
});
}
next();
});
app.use(function notFoundHandler(_req, res: express.Response) {
return res.status(404).send({ message: "Not Found" });
});
export const prisma = new PrismaClient();
export default app;