-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (42 loc) · 1.15 KB
/
app.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
43
44
45
46
47
48
49
50
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
app.use(cors({
origin: ['http://localhost:9528'],
}));
// GET: 取得熱門文章列表
app.get('/api/posts&before=:before', (req, res) => {
const before = req.params.before;
const apiUrl = before !== '0'
? `https://www.dcard.tw/v2/posts?before=${before}`
: `https://www.dcard.tw/v2/posts?popular=true`
return axios.get(apiUrl)
.then(({ status, statusText, headers, data }) => {
if (status === 200) {
return res.json(data);
} else {
return '';
}
}).catch((err) => {
res.send(err);
});
});
// GET: 取得該篇文章內容
app.get('/api/post&id=:id', (req, res) => {
const id = req.params.id;
const apiUrl = `https://www.dcard.tw/v2/posts/${id}`
return axios.get(apiUrl)
.then(({ status, statusText, headers, data }) => {
if (status === 200) {
return res.json(data);
} else {
return '';
}
}).catch((err) => {
res.send(err);
});
});
app.listen(9527, () => {
console.log('CORS-enabled web server listening on port 9527');
});