Vercel使用Express

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"
    }
  ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值