Simplify code

pull/1/head
Hans362 5 years ago
parent 2607c87c15
commit c19e430abc
Signed by: hans362
GPG Key ID: B186D77ABEC2A785

@ -3,7 +3,7 @@ const url = require('url');
const express = require('express'); const express = require('express');
const app = express(); const app = express();
const myhost = "api.bilibili.com"; const apiHost = "api.bilibili.com";
class Url { class Url {
getParam(data) { getParam(data) {
@ -19,8 +19,8 @@ class Url {
} }
} }
function nodePostGetRequest(HOST, PORT, method, bodydata, callBackFunction, path, cookie, ressss) { function nodePostGetRequest(HOST, PORT, method, bodyData, callBackFunction, path, cookie, result) {
var body = bodydata; var body = bodyData;
var bodyString = JSON.stringify(body); var bodyString = JSON.stringify(body);
var headers = { var headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -42,33 +42,31 @@ function nodePostGetRequest(HOST, PORT, method, bodydata, callBackFunction, path
}); });
res.on('end', function () { res.on('end', function () {
let resultObject = JSON.parse(responseString); let resultObject = JSON.parse(responseString);
getsuccess(resultObject, ressss); getSuccess(resultObject, result);
}); });
req.on('error', function (e) { req.on('error', function (e) {
console.log('-----error-------', e); console.log('[Error] ', e);
}); });
}); });
req.write(bodyString); req.write(bodyString);
req.end(); req.end();
} }
function getsuccess(data, res) { function getSuccess(data, res) {
res.send(data); res.send(data);
} }
app.get('/api', (req, res) => { app.get('/api', (req, res) => {
const type = req.query.type || "1"; const type = req.query.type || "1";
const pn = req.query.pn || "1"; const pn = req.query.pn || "1";
const ps = req.query.ps || "10"; const ps = req.query.ps || "30";
const vmid = req.query.vmid || "66745436"; const vmid = req.query.vmid;
const num = 28; const userCookie = req.query.cookies || "Disable cookies.";
const mycookie = req.query.cookies || "cookies";
let URL = new Url(); let URL = new Url();
let mypath = URL.getUrl("/x/space/bangumi/follow/list", { type: type, pn: pn, ps: ps, vmid: vmid }); let apiPath = URL.getUrl("/x/space/bangumi/follow/list", { type: type, pn: pn, ps: ps, vmid: vmid });
console.log(mypath); nodePostGetRequest(apiHost, 80, 'GET', null, getSuccess, apiPath, userCookie, res);
nodePostGetRequest(myhost, 80, 'GET', null, getsuccess, mypath, mycookie, res);
}); });
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running on ${port}, http://localhost:${port}`)); app.listen(port, () => console.log(`[INFO] Server running on ${port}, http://localhost:${port}`));

Loading…
Cancel
Save