easy-express-cwa
Version:
CLI tool to setup a common Express.js backend developed by codewithashim
47 lines (38 loc) • 1.11 kB
text/typescript
import cors from 'cors';
import express, { Application, NextFunction, Request, Response } from 'express';
import httpStatus from 'http-status';
import globalErrorHandler from './app/middlewares/globalErrorHandler';
import routes from './app/routes';
import cookieParser from 'cookie-parser';
import path from 'path';
const app: Application = express();
app.use(cors());
//parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use('/api/v1/', routes);
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use(globalErrorHandler);
app.get('/', (req: Request, res: Response) => {
res.json({
sucess: true,
message: 'Welcome to Doctor Portal Server',
status: httpStatus.OK,
})
});
//handle not found
app.use((req: Request, res: Response, next: NextFunction) => {
res.status(httpStatus.NOT_FOUND).json({
success: false,
message: 'Not Found',
errorMessages: [
{
path: req.originalUrl,
message: 'API Not Found',
},
],
});
next();
});
export default app;