-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (60 loc) · 1.51 KB
/
index.js
File metadata and controls
62 lines (60 loc) · 1.51 KB
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
const http = require("http");
const server = http.createServer();
const url = require("url");
const querystring = require("querystring");
const remoteUrl = "http://news-at.zhihu.com"
server.on('request', function(req, res) {
var urlOption = url.parse(req.url);
var pathName = urlOption.pathname;
if(/^\/api/.test(pathName)){
var request = http.request(remoteUrl + pathName);
res.setHeader("Access-Control-Allow-Origin","*");
request.on('response', function(response) {
var c = "";
response.setEncoding('utf8');
response.on('data', function(chunk) {
c += chunk;
});
response.on('end', function() {
res.writeHead(200, response.headers);
res.write(c);
res.end();
})
});
request.on('error', function(err) {
res.writeHead(200, {
"Content-Type": "application/json"
});
res.end({ "err": err });
});
request.end();
}else if(/\.(jpg|png)$/.test(pathName)){
const remoteImage="pic1.zhimg.com";
let headers = {
"Referer": "https://daily.zhihu.com/"
};
let opt={
hostname:remoteImage,
port:'80',
path:pathName,
headers:headers
}
let request = http.request(opt);
request.on('response', function(response) {
var c = "";
response.setEncoding('binary');
response.on('data', function(chunk) {
c += chunk;
});
response.on('end', function() {
res.writeHead(200, response.headers);
res.write(c, "binary");
res.end();
})
});
request.end();
}
});
server.listen(9999, function() {
console.log("server on port 9999");
});