server-getscheme.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const express = require('express')
  2. const app = express()
  3. const port = 3000
  4. app.get('/', (req, res) => {
  5. res.send('Hello World!欢迎来到协议提取接口服务器')
  6. })
  7. app.get('/scheme', async (req, res) => {
  8. let copy = req.query.copy
  9. if(!copy){
  10. res.json({
  11. code: 500,
  12. error:"您缺少copy参数"
  13. })
  14. return
  15. }
  16. console.log(req.query.copy)
  17. let schemeURI = await copyToScheme(copy)
  18. res.json({
  19. code: 200,
  20. data:{
  21. scheme:schemeURI
  22. }
  23. })
  24. })
  25. app.listen(port, () => {
  26. console.log(`Scheme API listening on port http://127.0.0.1:${port}`)
  27. })
  28. async function copyToScheme(copy){
  29. let shopShareLink = copy
  30. console.log(shopShareLink)
  31. let link = extractLinks(shopShareLink)[0]
  32. console.log(link)
  33. let response = await fetch(link)
  34. // let text = await response.text()
  35. let meituanURL = new URL(response.url)
  36. console.log(meituanURL)
  37. let schemeURI = meituanURL.searchParams.get("url")
  38. console.log(schemeURI)
  39. return schemeURI
  40. }
  41. function extractLinks(text) {
  42. // 正则表达式匹配HTTP/HTTPS链接
  43. // 匹配规则:
  44. // - 以http://或https://开头
  45. // - 包含域名(字母、数字、点、下划线、连字符)
  46. // - 可以包含路径、查询参数和锚点
  47. const urlRegex = /https?:\/\/[^\s"'<>]+/g;
  48. // 执行匹配并返回结果数组
  49. // 如果没有找到链接,返回空数组
  50. return text.match(urlRegex) || [];
  51. }