-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (46 loc) · 1.29 KB
/
index.js
File metadata and controls
60 lines (46 loc) · 1.29 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
55
56
57
58
59
60
import express from 'express';
import cookieParser from 'cookie-parser';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
// import routes
import recipeRoutes from './routes/recipe.js';
// set port
const PORT = process.env.PORT || 5000;
// 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(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
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}`);
});