forked from HackYourFutureBelgium/node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (41 loc) · 1.2 KB
/
index.js
File metadata and controls
54 lines (41 loc) · 1.2 KB
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
53
54
import express from 'express';
import cookieParser from 'cookie-parser';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import createUserTable from './models/user.js';
import createRecipeTable from './models/recipe.js';
// import routes
import userRoutes from './routes/user.js';
import recipeRoutes from './routes/recipe.js';
// set port
const PORT = process.env.PORT || 5009;
// Construct path
const __filename = fileURLToPath(import.meta.url);
const PATH = dirname(__filename);
// initialize express
const app = express();
// parse body and cookies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Serve static files
app.use(express.static(path.join(PATH, 'public')));
// create tables
createUserTable();
createRecipeTable();
// use routes
app.use(userRoutes);
app.use(recipeRoutes);
// error
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ message: 'Internal Server Error' });
});
// handle 404
app.use('*', (req, res) => {
res.status(404).json({ message: 'Page is not found' });
});
// listen
app.listen(PORT, () => {
console.log(`Server is up and running on port : ${PORT}`);
});