-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyServer.js
45 lines (36 loc) · 1.4 KB
/
myServer.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
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var logger = require('morgan');
var fs = require('fs');
var logFile = fs.createWriteStream('./logOfMyServer.log', {flags: 'a'});
var app = express();
app.use(express.static(__dirname));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(logger({stream: logFile}));
app.post('/proxy', function(req, res) {
var requestUrl = req.body.requestUrl;
var requestContent = req.body.requestContent;
logFile.write("[" + new Date() + "] Request Url: [" + requestUrl + "]. From: [" + req.ip + "].\n");
logFile.write("[" + new Date() + "] Request Content: [" + JSON.stringify(requestContent) + "].\n");
doPost(requestUrl, JSON.stringify(requestContent), function (res2) {
logFile.write("[" + new Date() + "] Response Content: [" + JSON.stringify(res2) + "].\n");
res.end(res2);
});
});
var doPost = function(url, content, callback) {
var options = {
uri: url,
method: 'POST',
body: content,
rejectUnauthorized: false
};
request(options, function (error, response, body) {
var res = error? JSON.stringify(error): body;
callback(res);
});
};
app.listen(process.env.PORT || 80); // need to run as root
app.listen(process.env.PORT || 3000);
console.log("Server running at port 3000 and 80");