-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (91 loc) · 2.98 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const request = require('request');
const app = express();
const Router = require('react-router');
app.set('port', (process.env.PORT || 3000));
app.use('/', express.static(path.join(__dirname)));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Additional middleware which will set headers that we need on each request.
app.use((req, res, next) => {
// Set permissive CORS header - this allows this server to be used only as
// an API server in conjunction with something like webpack-dev-server.
res.setHeader('Access-Control-Allow-Origin', '*');
// Disable caching so we'll always get the latest.
res.setHeader('Cache-Control', 'no-cache');
next();
});
app.get('/api/users/:id?', (req, res) => {
let userUrl = 'http://spa.tglrw.com:4000/users/';
if (req.params.id) {
// Add id to user query
userUrl += req.params.id;
}
request(userUrl, (err, response, body) => {
if (err) {
console.error(userUrl, ': Returned statusCode:', err.statusCode, err);
} else {
console.info('GET USERS SUCCESSFUL:');
res.json(JSON.parse(body));
}
});
});
app.get('/api/widgets/:id?', (req, res) => {
let widgetUrl = 'http://spa.tglrw.com:4000/widgets/';
if (req.params.id) {
// Add id to user query
widgetUrl += req.params.id;
}
request(widgetUrl, (err, response, body) => {
if (err) {
console.error(widgetUrl, ': Returned statusCode:', err.statusCode, err);
} else {
console.info('GET WIDGETS SUCCESSFUL:');
res.json(JSON.parse(body));
}
});
});
app.post('/api/widgets/', (req, res) => {
const postWidgetUrl = 'http://spa.tglrw.com:4000/widgets';
const options = {
json: req.body,
};
request.post(postWidgetUrl, options, (err, response, body) => {
if (err) {
console.error(postWidgetUrl, ': Returned statusCode:', err.statusCode, err);
} else {
console.log('POST SUCCESSFUL');
console.log('POST response:', body);
res.json(body);
}
});
});
app.put('/api/widgets/:id', (req, res) => {
const putWidgetUrl = `http://spa.tglrw.com:4000/widgets/${req.params.id}`;
const options = {
json: req.body,
};
request.put(putWidgetUrl, options, (err, response, body) => {
if (err) {
console.error(putWidgetUrl, ': Returned statusCode:', err.statusCode, err);
} else {
console.log('PUT SUCCESSFUL');
console.log('PUT response:', body);
res.send('Updated!');
}
});
});
// app.get('*', function (req, res) { // This wildcard method handles all requests
//
// Router.run(routes, req.path, function (Handler, state) {
// var element = React.createElement(Handler);
// var html = React.renderToString(element);
// res.render('main', { content: html });
// });
// });
app.listen(app.get('port'), () => {
console.log(`Server started: http://localhost:${app.get('port')}/`);
});