index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import express from 'express'
  2. import qs from 'qs'
  3. import axios from 'axios'
  4. import { commonParams, paramsSign } from '../common/net.js'
  5. import { php_api_seo_ids } from '../common/apis.js'
  6. const phpResponseModel = {
  7. code: -1,
  8. msg: 'Sorry,something wrong with your network~',
  9. data: {}
  10. }
  11. // Create express router
  12. const router = express.Router()
  13. // Transform req & res to have the same API as express
  14. // So we can use res.status() & res.json()
  15. const app = express()
  16. router.use((req, res, next) => {
  17. Object.setPrototypeOf(req, app.request)
  18. Object.setPrototypeOf(res, app.response)
  19. req.res = res
  20. res.req = req
  21. next()
  22. })
  23. const sitemapLogic = async (req, res) => {
  24. const concatXml = obj => {
  25. if (!obj) {
  26. return ''
  27. }
  28. const { article } = obj
  29. let s = `<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"><url><loc>http://bbs.hoolihome.com</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url><url><loc>http://bbs.hoolihome.com/en/</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url><url><loc>http://bbs.hoolihome.com/videos/</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url><url><loc>http://bbs.hoolihome.com/en/videos/</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url>`
  30. article.forEach(v => {
  31. s += `<url><loc>http://bbs.hoolihome.com/article/${v}</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url><url><loc>http://bbs.hoolihome.com/en/article/${v}</loc><priority>1.00</priority><lastmod>2019-05-07</lastmod><changefreq>daily</changefreq></url>`
  32. })
  33. s += `</urlset>`
  34. return s
  35. }
  36. const params = {}
  37. params.url = php_api_seo_ids
  38. params.data = {
  39. type: 1
  40. }
  41. Object.assign(params.data, commonParams())
  42. params.data.signToken = paramsSign(params.data, params.url)
  43. phpResponseModel.data = {}
  44. const phpResponse = await axios
  45. .post(params.url, qs.stringify(params.data))
  46. .then(result => {
  47. if (result) {
  48. let { status, data } = result
  49. if (parseInt(status) === 200 && data) {
  50. let { code } = data
  51. if (parseInt(code) === 0) {
  52. return concatXml(data.data)
  53. }
  54. }
  55. }
  56. return ''
  57. })
  58. .catch(e => {
  59. return ''
  60. })
  61. res.type('application/xml')
  62. res.status(200).send(phpResponse)
  63. }
  64. router.get('/sitemap.xml', sitemapLogic)
  65. // Export the server middleware
  66. export default {
  67. path: '/',
  68. handler: router
  69. }