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.xx,req.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")) }