File size: 2,129 Bytes
0414e50 |
1 |
const express = require('express');const rateLimit = require('express-rate-limit');const helmet = require('helmet');const bodyParser = require("body-parser");const xss = require('xss-clean');const hpp = require('hpp');const cors = require('cors');const multer = require('multer');const upload = multer();const session = require('express-session');const { secret } = require('./config/common/config');// Importing The Databaseconst postgresql = require('./config/database/postgresqlConfig');// Importing the Routes const covidRoutes = require('./routes/covid/covidRoutes')// Calling the Error Handlersconst globalErrHandler = require('./controllers/error/errorController');const AppError = require('./utils/error/appError');const app = express();// Allow Cross-Origin requestsapp.use(cors());// Set security HTTP headersapp.use(helmet());// Limit request from the same API const limiter = rateLimit({ max: 150, windowMs: 60 * 60 * 1000, message: 'Too Many Request from this IP, please try again in an hour'});app.use('/api/v1', limiter);// Body parser, reading data from body into req.bodyapp.use(express.json({ limit: '100kb'}));// Configure the Session expiryapp.use( // BUG: CWE-522: Insufficiently Protected Credentials// session({ secret: secret, resave: true, saveUninitialized: false })// FIXED: );// Data sanitization against XSS(clean user input from malicious HTML code)app.use(xss());// Prevent parameter pollutionapp.use(hpp());// Setting Body parserapp.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: true }));app.use(bodyParser.json({ type: 'application/*+json' }));app.use(express.json());app.use(upload.array()); app.use(express.static('public'));// Setting up global error handlerapp.use(globalErrHandler);// Checking and Testing of Homeapp.get('/', (req, res) => { // console.log(req.session) res.send(`You hit home page!\n`)})// Routes Final Callingapp.use('/api/v1',covidRoutes);// handle undefined Routesapp.use('*', (req, res, next) => { const err = new AppError(404, 'fail', 'undefined route'); next(err, req, res, next);});module.exports = app; |