Files
express_router/index.js
2024-09-27 01:21:21 +08:00

44 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require("axios");
const router = require("./run");
const config = {
BaseUrl: 'https://mbizhi.cheetahfun.com/sj',
list: {
search(keyword = "jk", page = 1) {
return `${config.BaseUrl}/search.html?search=${keyword}&page=${page}`
}
}
}
/**
* 闲着蛋疼使用原生nodejs模仿 express 自己实现了 express 的路由
* 已实现 get post 方法支持,和 参数解析 req.query.xxreq.body.xx
*/
// http://127.0.0.1:3000/search?keyword=jk&page=1
router.get("/search",async (req,res) => {
res.setHeader('Content-Type','application/json; charset=UTF-8');
let { keyword, page } = req.query
let result = await get(config.list.search(keyword,page));
res.json(result)
})
// router.get("/test",async (req,res) => {
// res.setHeader('Content-Type','application/json; charset=UTF-8');
// res.json(req.query)
// })
// router.post("/test",async (req,res) => {
// res.setHeader('Content-Type','application/json; charset=UTF-8');
// res.json(req.body)
// })
const get = async (url) => {
let res = await axios.get(url);
// 通过分析 元气桌面 网站源码发现当前页面的json接口数据是页面中的script代码运行后的函数返回值
// 所以先 match 提取 window.__NUXT__ 和 )) 中间的 核心js代码因为nodejs没有window所以先将代
// 码 中 window 替换为 config然后 eval 执行对方网站的 代码,拿到方法返回值,也就是我们的 【接口数据】
return eval(res.data.match(/window.__NUXT__([\s\S]*?)\)\);/)[0].replace("window", "config"))
}