惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

WishMeLz - NodeJS

Electron 主进程起一个可用的 HTTPS 静态服务器 - WishMeLz 宝塔Node管理器安装node版本提示:文件下载失败,请手动安装! - WishMeLz Minio 之 Nodejs - WishMeLz nuxt项目本地启动,多开标签一直显示加载中 - WishMeLz nvm win10安装 - WishMeLz nodejs 对接邮箱服务 imap - WishMeLz PM2搭配nvm使用不同版本Node启动项目 - WishMeLz EA Racenet API - WishMeLz SSE(Server-Sent Events) - WishMeLz
nodejs 生成网站sitemap.xml - WishMeLz
WishMeLz · 2026-06-01 · via WishMeLz - NodeJS
const mysql = require('mysql2')
const fs = require('fs')
const xml2js = require('xml2js')
const path = require('path') // 引入 path 模块以处理路径

// 配置数据库连接
const dbConfig = {
    host: 'localhost',
    port: '3306',
    user: '',
    password: '',
    database: '',
}

// 创建数据库连接
const pool = mysql.createPool(dbConfig)

// 查询数据库获取数据
function fetchDataFromDB() {
    return new Promise((resolve, reject) => {
        pool.query(
            'SELECT id, created_at, updated_at, title FROM simple_topic ORDER BY id DESC',
            (err, results) => {
                if (err) {
                    return reject(err)
                }
                resolve(results)
            },
        )
    })
}

// 生成sitemap.xml文件
async function generateSitemap(data) {
    const urlset = {
        urlset: {
            $: { xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' },
            url: data.map((row) => ({
                loc: [`https://xxxx.com/t/${row.id}.html`],
                lastmod: [
                    new Date(row.updated_at * 1000).toLocaleDateString('en-CA'),
                ], 
                changefreq: ['monthly'],
                priority: ['0.5'],
            })),
        },
    }

    const builder = new xml2js.Builder()
    const xml = builder.buildObject(urlset)

    // 获取脚本文件所在目录的上一级目录
    const outputDir = path.join(__dirname, '..', 'sitemap.xml')

    // 将 XML 写入上一级目录
    fs.writeFileSync(outputDir, xml, 'utf-8')
    console.log('Sitemap generated successfully at:', outputDir)
}

async function start() {
    try {
        const data = await fetchDataFromDB()
        if (data.length) {
            await generateSitemap(data)
        } else {
            console.log('No data found to generate sitemap.')
        }
    } catch (error) {
        console.error('Error fetching data or generating sitemap:', error)
    }
}

start()

console.log(new Date().toLocaleDateString('en-CA'))

console.log('Sitemap generator running...')