forked from freeCodeCamp/boilerplate-project-exercisetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (41 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'dotenv/config.js';
import apiRouter from './routes/api/index.js';
import cors from 'cors';
import express from 'express';
import { fileURLToPath } from 'url';
import { handleError } from './middleware/error-handler.middleware.js';
import { httpStatusCodes } from './helpers/httpStatusCodes.js';
import indexRouter from './routes/index.js';
import { initAppDatabase } from './database/database.js';
import path from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 3000;
const app = express();
await initAppDatabase();
app.use(cors());
// For parsing application/json
app.use(express.json());
// For parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/api', apiRouter);
// NOT FOUND GLOBAL HANDLER
app.use((req, res) => {
res.status(httpStatusCodes.NOT_FOUND).json({
success: 'false',
message: `Route ${req.url} with ${req.method} method is not found`,
error: {
statusCode: httpStatusCodes.NOT_FOUND,
message: 'You reached a route that is not defined on this server',
},
});
});
// THROW ErrorHandler
app.use((err, req, res) => {
handleError(err, res);
});
const listener = app.listen(process.env.PORT || PORT, () => {
console.log('Your app is listening on port ' + listener.address().port);
});