up seo
This commit is contained in:
36
server/server-example-express.js
Normal file
36
server/server-example-express.js
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 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
|
||||
Reference in New Issue
Block a user