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');
|
2023-02-19 07:05:36 +01:00
|
|
|
const bodyparser = require('body-parser');
|
2023-02-19 06:30:02 +01:00
|
|
|
|
2023-02-26 15:18:26 +01:00
|
|
|
const { APP_PORT } = process.env;
|
2023-02-13 02:16:46 +01:00
|
|
|
|
2023-02-19 07:05:36 +01:00
|
|
|
app.use(bodyparser.json());
|
|
|
|
app.use(bodyparser.urlencoded({ extended: true }));
|
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
|
2023-02-22 07:42:06 +01:00
|
|
|
app.use(async (req, res, next) => {
|
2023-03-05 15:26:56 +01:00
|
|
|
if (res.headersSent) return next();
|
2023-02-19 07:54:38 +01:00
|
|
|
|
2023-03-05 15:26:56 +01:00
|
|
|
res.status(404);
|
|
|
|
res.template('error.pug', {
|
|
|
|
error: '404 Not Found',
|
|
|
|
message: 'The requested page was not found.'
|
|
|
|
});
|
|
|
|
})
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(500);
|
2023-03-05 15:26:56 +01:00
|
|
|
res.template('error.pug', {
|
2023-02-19 07:54:38 +01:00
|
|
|
error: '500 Internal Server Error',
|
|
|
|
message: 'An unexpected error happened in the server'
|
2023-03-05 15:26:56 +01:00
|
|
|
});
|
2023-02-19 07:54:38 +01:00
|
|
|
})
|
|
|
|
|
2023-02-13 02:16:46 +01:00
|
|
|
const server = app.listen(APP_PORT, () => {
|
|
|
|
console.log("Listening on port " + APP_PORT);
|
|
|
|
});
|