Files
tyc-webview-v2/server/server-example-express.js
2026-02-28 12:45:13 +08:00

37 lines
899 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Express服务器示例
* 展示如何集成SEO中间件
*/
const express = require('express')
const path = require('path')
const SEOMiddleware = require('./middleware')
const app = express()
const port = process.env.PORT || 3000
// 初始化SEO中间件
const seoMiddleware = new SEOMiddleware({
templateDir: path.join(__dirname, '../public/seo-templates'),
debug: process.env.NODE_ENV === 'development'
})
// 应用SEO中间件必须在静态文件服务之前
app.use(seoMiddleware.express())
// 静态文件服务
app.use(express.static(path.join(__dirname, '../dist')))
// SPA路由处理
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../dist/index.html'))
})
// 启动服务器
app.listen(port, () => {
console.log(`🚀 服务器运行在 http://localhost:${port}`)
console.log(`🔍 SEO中间件已启用`)
})
module.exports = app