-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.9 KB
/
index.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
const http = require("http");
const chalk = require("chalk");
const cralwer = require("./crawler");
const urlParser = require("url");
const path = require("path");
const fs = require("fs");
const server = http.createServer((msg, res) => {
const { url } = msg;
let { pathname, query: source } = urlParser.parse(url);
const { q: query, limit } = require("querystring").parse(source);
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
res.setHeader("Access-Control-Allow-Methods", "GET,POST");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Connection", "keep-alive");
res.setHeader("Transfer-Encoding", "chunked");
res.statusCode = 200;
//限制最大图片获取数量为100
let number = limit > 100 ? 100 : limit;
routeUrl(res, pathname, { query, limit: number });
});
server.listen(5000, "localhost", () => {
console.log(chalk.green("服务器工作在:http://localhost:5000"));
});
function routeUrl(res, route, query) {
if (route === "/") {
let html = fs.readFileSync(path.resolve(__dirname, "fe", "gallery.html"), { encoding: "utf-8" });
res.setHeader("Content-Type", "text/html; charset:utf-8");
res.end(html);
} else if (route === "/favicon.ico") {
return;
} else if (route === "/renderImg.js") {
let js = fs.readFileSync(path.resolve(__dirname, "fe", "renderImg.js"), { encoding: "utf-8" });
res.setHeader("Content-Type", "text/javascript; charset:utf-8");
res.end(js);
} else if (route === "/data/") {
res.setHeader("Content-Type", "application/json; charset:utf-8");
cralwer(query, false)
.then(data => {
if (!data) {
data = JSON.stringify({});
}
res.end(data);
})
.catch(err => {
res.end(err);
});
}
}