-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (27 loc) · 857 Bytes
/
server.js
File metadata and controls
43 lines (27 loc) · 857 Bytes
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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import posts from './routes/posts.js';
import logger from './middleware/logger.js';
import errorHandler from './middleware/error.js';
import notFound from './middleware/notFound.js';
const port = process.env.PORT || 5000;
const app = express();
//Body parser middware
app.use(express.json());
app.use(express.urlencoded({ extended: false}));
//Logger middleware
app.use(logger);
//Setup static folder
app.use(express.static(path.join(__dirname, 'public')));
//Routes
app.use('/api/posts', posts);
//Test route
app.get('/api/test', (req, res) => {
res.json({ message: 'Test route works!' });
});
//Not Found Handler
app.use(notFound);
//Error Handler
app.use(errorHandler);
app.listen(port, () => console.log(`Server is running on port ${port}`));