-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
26 lines (21 loc) · 761 Bytes
/
Copy pathserver.js
File metadata and controls
26 lines (21 loc) · 761 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
const express = require('express');
const path = require('path');
const app = express();
const {articles} = require('./src/articles.json')
const {comments} = require('./src/comments.json')
if(process.env.NODE_ENV !== 'development') {
app.use(express.static(path.join(__dirname, 'build')));
app.get('/article(/*)', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
})
}
app.get('/api/articles', (req, res) => {
res.status(200).json({articles});
})
app.get('/api/articles/:id/comments', (req, res) => {
const artId = req.params.id
const artCommets = comments.filter(comment => comment.articleId == artId)
res.status(200).json({comments: artCommets});
})
app.listen(8080);
console.log('Serving from port 8080')