homepage.js/index.js

75 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-02-13 02:16:46 +01:00
// do startup jobs
require('./startup');
2023-02-19 15:19:46 +01:00
if (process.env.APP_DEBUG == 'true') {
process.env.DEBUG = '*/*';
process.env.NODE_ENV = 'development';
} else {
process.env.NODE_ENV = 'production';
}
2023-02-13 02:16:46 +01:00
const express = require('express');
const app = express();
2023-02-18 16:29:43 +01:00
const session = require('express-session');
const cookie_parse = require('cookie-parser');
const cookie_encrypt = require('cookie-encrypter');
2023-02-19 03:25:01 +01:00
const Redis = require("ioredis");
2023-02-19 07:05:36 +01:00
const bodyparser = require('body-parser');
2023-02-19 06:30:02 +01:00
2023-02-19 03:25:01 +01:00
let redisClient = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST);
let RedisStore = require("connect-redis")(session)
2023-02-13 02:16:46 +01:00
2023-02-18 16:29:43 +01:00
const { APP_PORT, APP_KEY } = process.env;
2023-02-13 02:16:46 +01:00
2023-02-20 03:43:53 +01:00
app.use((req, res, next) => {
req.start = Date.now();
res.on('header', (res) => {
let time = Date.now() - req.start;
console.log(time)
res.setHeader('X-Reponse-Time', time);
})
next();
});
2023-02-19 07:05:36 +01:00
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
2023-02-18 16:29:43 +01:00
app.use(cookie_parse(APP_KEY))
app.use(cookie_encrypt(APP_KEY));
app.use(session({
secret: APP_KEY,
2023-02-19 03:25:01 +01:00
resave: false,
saveUninitialized: true,
cookie: { secure: true },
store: new RedisStore({ client: redisClient })
2023-02-18 16:29:43 +01:00
}));
2023-02-19 03:25:01 +01:00
app.use(require('./middleware'));
2023-02-13 02:16:46 +01:00
2023-02-19 06:30:02 +01:00
app.use(require('./routes'));
app.use(express.static('public'));
// 404
app.use((req, res, next) => {
})
2023-02-19 07:54:38 +01:00
// error handler
app.use(async (err, req, res, next) => {
console.log(err);
if (res.headersSent) {
return next(err);
}
const Helpers = require('./helpers');
res.status(500);
res.send(await Helpers.ViewLoader.load('error.pug', {
error: '500 Internal Server Error',
message: 'An unexpected error happened in the server'
}));
})
2023-02-13 02:16:46 +01:00
const server = app.listen(APP_PORT, () => {
console.log("Listening on port " + APP_PORT);
});