Skip to content

Instantly share code, notes, and snippets.

@robozavri
Last active September 10, 2020 13:14
Show Gist options
  • Save robozavri/1b0a91c02198cba44a3316efeee1276f to your computer and use it in GitHub Desktop.
Save robozavri/1b0a91c02198cba44a3316efeee1276f to your computer and use it in GitHub Desktop.
#express #node.js
import { Express } from 'express';
import compression from 'compression';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import cors from '../config/cors';
import ejs from 'ejs';
import passport from 'passport';
export function initExpress(app: Express) {
app.disable('x-powered-by');
app.set('view engine', 'ejs');
app.engine('.html', ejs.renderFile);
app.use(compression());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '5gb', extended: true}));
// app.use(bodyParser.urlencoded({ extended: false }));
// app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors);
app.use(passport.initialize());
}
// config/cors
import { Request, Response, NextFunction } from 'express';
export default function (req: Request, res: Response, next: NextFunction) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, PATCH, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Token');
next();
}
////
import express from 'express';
import {initExpress} from './express';
import {usersRouter} from './user';
import * as auth from './auth';
const app = express();
const PORT = 8000;
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
initExpress(app);
app.use(auth.setUser);
app.use('/users', usersRouter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment