-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
43 lines (39 loc) · 1.03 KB
/
server.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
const express = require('express');
const app = express();
const request = require('request');
const port = 3001;
const bodyParser = require('body-parser');
const cors = require('cors');
app.options('*', cors());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));
app.get('/', (req, res) => res.sendFile('public/index.html'));
app.get('/todo', (req, res) => {
request('http://localhost:3000/todo', (err, response, body) => {
if (response.statusCode <= 500) {
res.send(body);
}
});
});
app.post('/todo', (req, res) => {
let body = req.body.body;
const todo = {
body: body
};
const headersOpt = {
'content-type': 'application/json'
};
let x = {
method: 'post',
url: 'http://localhost:3000/todo',
body: todo,
headers: headersOpt,
json: true
};
request.post(x, (err, response, body) => {
res.send(response.body);
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));