123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import express from 'express'
- import qs from 'qs'
- import axios from 'axios'
- import { commonParams, paramsSign } from '../common/net.js'
- import { php_api_seo_ids } from '../common/apis.js'
- const phpResponseModel = {
- code: -1,
- msg: 'Sorry,something wrong with your network~',
- data: {}
- }
- // Create express router
- const router = express.Router()
- // Transform req & res to have the same API as express
- // So we can use res.status() & res.json()
- const app = express()
- router.use((req, res, next) => {
- Object.setPrototypeOf(req, app.request)
- Object.setPrototypeOf(res, app.response)
- req.res = res
- res.req = req
- next()
- })
- const sitemapLogic = async (req, res) => {
- const concatXml = obj => {
- if (!obj) {
- return ''
- }
- const { article } = obj
- 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>`
- article.forEach(v => {
- 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>`
- })
- s += `</urlset>`
- return s
- }
- const params = {}
- params.url = php_api_seo_ids
- params.data = {
- type: 1
- }
- Object.assign(params.data, commonParams())
- params.data.signToken = paramsSign(params.data, params.url)
- phpResponseModel.data = {}
- const phpResponse = await axios
- .post(params.url, qs.stringify(params.data))
- .then(result => {
- if (result) {
- let { status, data } = result
- if (parseInt(status) === 200 && data) {
- let { code } = data
- if (parseInt(code) === 0) {
- return concatXml(data.data)
- }
- }
- }
- return ''
- })
- .catch(e => {
- return ''
- })
- res.type('application/xml')
- res.status(200).send(phpResponse)
- }
- router.get('/sitemap.xml', sitemapLogic)
- // Export the server middleware
- export default {
- path: '/',
- handler: router
- }
|