-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
52 lines (44 loc) · 1.24 KB
/
proxy.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
var express = require('express'),
bodyParser = require('body-parser'),
cors = require('cors'),
http = require('http'),
querystring = require('querystring'),
app = express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(cors());
app.get('/*', function(req, res, next) {
console.log(req.url);
console.log(req.query);
res.json({
msg: 'This is CORS-enabled for all origins!'
});
});
app.post('/*', function(req, res, next) {
// console.log(req.url);
// console.log(req.body);
var postData = req.body;
// the post options
var opts = {
host: 'dev.erikqin.com',
port: '80',
path: req.url,
method: 'POST'
};
// 服务器端发送REST请求
var reqPost = http.request(opts, function(resPost) {
console.log(res.statusCode);
console.log(JSON.stringify(resPost.headers));
resPost.on('data', function(d) {
return res.send(d);
});
});
reqPost.write(querystring.stringify(postData));
reqPost.end();
reqPost.on('error', function(e) {
console.error(e.message);
});
});
app.listen(9000, function() {
console.log('CORS-enabled web server listening on port 9000');
});