source: src/app.js

main
Last change on this file was 7517a3a, checked in by Luka Krstikj <luka.krstik@…>, 23 hours ago

init

  • Property mode set to 100644
File size: 1.3 KB
Line 
1require('dotenv').config();
2const express = require('express');
3const path = require('path');
4const database = require('./config/database');
5const routes = require('./routes');
6
7const app = express();
8const PORT = process.env.PORT || 3000;
9
10app.set('view engine', 'ejs');
11app.set('views', path.join(__dirname, '../views'));
12
13app.use(express.json());
14app.use(express.urlencoded({ extended: true }));
15app.use(express.static(path.join(__dirname, '../public')));
16app.use('/', routes);
17
18app.use((req, res) => {
19 res.status(404).render('error', {
20 message: 'Page not found',
21 error: { status: 404 }
22 });
23});
24
25app.use((err, req, res, next) => {
26 console.error(err.stack);
27 res.status(500).render('error', {
28 message: 'Something went wrong!',
29 error: process.env.NODE_ENV === 'development' ? err : {}
30 });
31});
32
33async function startServer() {
34 try {
35 await database.connect();
36
37 app.listen(PORT, () => {
38 console.log(`Server running on http://localhost:${PORT}`);
39 });
40 } catch (error) {
41 console.error('Failed to start server:', error);
42 process.exit(1);
43 }
44}
45
46process.on('SIGINT', async () => {
47 console.log('\nShutting down gracefully...');
48 await database.close();
49 process.exit(0);
50});
51
52process.on('SIGTERM', async () => {
53 console.log('\nShutting down gracefully...');
54 await database.close();
55 process.exit(0);
56});
57
58startServer();
Note: See TracBrowser for help on using the repository browser.