refactor: 优化serverless入口文件

This commit is contained in:
ElyPrism 2026-06-18 22:29:24 +08:00
parent 395a80e74b
commit 1db354464f
No known key found for this signature in database
3 changed files with 33 additions and 6 deletions

8
app.js
View File

@ -8,11 +8,9 @@ async function start() {
if (!fs.existsSync(path.resolve(tmpPath, 'anonymous_token'))) { if (!fs.existsSync(path.resolve(tmpPath, 'anonymous_token'))) {
fs.writeFileSync(path.resolve(tmpPath, 'anonymous_token'), '', 'utf-8') fs.writeFileSync(path.resolve(tmpPath, 'anonymous_token'), '', 'utf-8')
} }
// 启动时更新anonymous_tokenVercel 构建环境下跳过网络请求喵~ // 启动时更新anonymous_token
if (!process.env.VERCEL_ENV) { const generateConfig = require('./generateConfig')
const generateConfig = require('./generateConfig') await generateConfig()
await generateConfig()
}
require('./server').serveNcmApi({ require('./server').serveNcmApi({
checkVersion: true, checkVersion: true,
}) })

View File

@ -1 +1,29 @@
require('./app.js') // Serverless 入口 + 本地开发通用入口
const fs = require('fs')
const path = require('path')
const tmpPath = require('os').tmpdir()
// 确保 anonymous_token 文件存在util/request.js 加载时需要读取)
if (!fs.existsSync(path.resolve(tmpPath, 'anonymous_token'))) {
fs.writeFileSync(path.resolve(tmpPath, 'anonymous_token'), '', 'utf-8')
}
const { constructServer } = require('./server')
const generateConfig = require('./generateConfig')
// Serverless 懒加载
let app = null
module.exports = async (req, res) => {
if (!app) {
await generateConfig()
app = await constructServer()
}
return app(req, res)
}
// Vercel判定
if (!process.env.VERCEL) {
require('./app.js')
}

View File

@ -456,5 +456,6 @@ async function serveNcmApi(options) {
module.exports = { module.exports = {
serveNcmApi, serveNcmApi,
constructServer,
getModulesDefinitions, getModulesDefinitions,
} }