Instantly share code, notes, and snippets.

Embed
What would you like to do?
import express from "express";
import logger from "morgan";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import session from "express-session";
import { SoftSwissApiRouter } from "./routes/SoftSwissApi";
const app = express();
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
const sessionMiddleware = session({
secret: "session secret",
resave: true,
saveUninitialized: true
});
app.use(sessionMiddleware);
app.use('/', SoftSwissApiRouter);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use((err, req, res) => {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: err,
});
});
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res) => {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: {},
});
});
export {
app,
sessionMiddleware
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment