-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 950 Bytes
/
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
const express = require('express');
const app = express();
const cors = require('cors');
const port = process.env.PORT || 5000;
app.use(cors());
const categories = require('./data/categories.json');
const news = require('./data/news.json');
app.get('/', (req, res) => {
res.send('News API Running');
});
app.get('/news-categories', (req, res) => {
res.send(categories)
});
app.get('/category/:id', (req, res) => {
const id = req.params.id;
if (id === '08') {
res.send(news);
}
else {
const category_news = news.filter(n => n.category_id === id);
res.send(category_news);
}
})
app.get('/news', (req, res) =>{
res.send(news);
});
//fsdgdfgsdfgfsd
app.get('/news/:id', (req, res) => {
const id = req.params.id;
const selectedNews = news.find(n => n._id === id);
res.send(selectedNews);
});
app.listen(port, () => {
console.log('Dragon News Server running on port', port);
})