Vercel使用Express
Express
vercel.json配置如下即可。
{
"builds": [
{
"src": "app.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "(.*)",
"dest": "app.js"
}
]
}
注意,服务不要监听3000端口,这个被vercel本身占用,换用其他,如3008,8080,13000等均可。
当使用@vercel/node
并且是CommonJS时候会出现listener is not a function
错误,原因是@vercel/node会自行分配PORT,并且需要你导出的app对象,所以需要使用下面方式,否则会多次listen从而挂掉。(ESM运行没问题)
如果希望使用新的@vercel/node,请注意app.js的配置,如下:
module.exports = app
// app.listen(PORT, () => {
// console.log(`服务器运行在 http://localhost:${PORT}`)
// })
// 如果有类似LeanCloud等需要初始化的业务,记得判断一下,只执行一次即可
// if (!AV.applicationId) {
// AV.init({
// appId: '-gzGzoHsz',
// appKey: 't3j',
// serverURL: 'https://xxx.yyy.cn',
// masterKey: '123123123'
// })
// }
Vercel 出现module not found
但本地确定检测无误后,请使用@vercel/node
。
如果使用CloudFlare时候,记得SSL/TLS配置中,从灵活改为完全,否则会遇到ERR_TOO_MANY_REDIRECTS
的问题。
超时问题
| | Default | Maximum |
|:------------:|:-------------:|:-------:|
| Hobby | 10 | 60 |
| Pro | 15 | 300 |
| EnterPrise | 15 | 900 |
更改Default时长:
{
"functions": {
"api/test.js": {
"maxDuration": 30 // This function can run for a maximum of 30 seconds
},
"api/*.js": {
"maxDuration": 15 // This function can run for a maximum of 15 seconds
},
"src/api/*.js": {
"maxDuration": 25 // You must prefix functions in the src directory with /src/
}
}
}
Vue
Vue子路径在history模式时候404问题。
vercel.json
{
"rewrites": [
{
"source": "/:path*",
"destination": "/index.html"
}
]
}