import createHttpError from 'http-errors'; import express from 'express'; import path from 'path'; import cookieParser from 'cookie-parser'; import logger from 'morgan'; import passport from 'passport'; import session from 'express-session'; import { nodeEnv, secretSession } from './config'; import passportConfig from './libs/passport'; import authRouter from './routes/v1/auth'; import meRouter from './routes/v1/me'; import usersRouter from './routes/v1/users'; const app = express(); passportConfig(passport); app.use(passport.initialize()); app.set('views', path.join(__dirname, '../views')); app.set('view engine', 'ejs'); app.set('trust proxy', 1); app.use( session({ secret: secretSession, resave: false, saveUninitialized: true, cookie: { secure: true }, }) ); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use(passport.session()); app.use('/v1/auth', authRouter); app.use('/v1/me', meRouter); app.use('/v1/users', usersRouter); // catch 404 and forward to error handler app.use((req, res, next) => { next(createHttpError(404)); }); // eslint-disable-next-line no-unused-vars app.use((err, req, res, next) => { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = nodeEnv === 'development' ? err : {}; // render the error page res.status(err.status || 500); if (req.xhr) { res.json(err.message); } else { res.render('error'); } }); module.exports = app;