-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
38 lines (31 loc) · 1.22 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
const connect = require('connect');
const serveStatic = require('serve-static');
const request = require('request');
const config = require('./config.js');
const bodyParser = require('body-parser');
const app = connect();
// var textBody = require("body");
// var jsonBody = require("body/json");
// var formBody = require("body/form");
// const rp = require('request-promise-native');
// app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.use('/api', (req, res) => {
const _path = req.originalUrl.substr(4);
const requestType = req.method;
// console.log(requestType + ": " + config.plasma_host + _path);
if (requestType === "GET") {
request.get({uri: config.plasma_host + _path, timeout: 2000}).on('error', e=>{
res.writeHead(408);
res.end()
}).pipe(res)
} else if (requestType === "POST") {
request.post({uri: config.plasma_host + _path, headers: {"Content-Type":"application/json"}, body: req.body, json: true, timeout: 2000}).on('error', e=>{
res.writeHead(408);
res.end()
}).pipe(res)
}
});
app.use(serveStatic(__dirname)).listen(9000, function(){
console.log('Server running on 9000...');
});