使用Python将多个路由合并到Vercel中的一个无服务器功能中?

时间:2020-06-21 21:58:25

标签: python flask next.js vercel sanic

我目前有一个带有python API后端的Nextjs应用。我遇到的问题是Vercel最多只能有24个无服务器功能,并且他们似乎建议您结合使用无服务器功能来“优化”您的功能并避免冷启动。

当前我有以下代码

from sanic import Sanic
from sanic.response import json
app = Sanic()


@app.route('/')
@app.route('/<path:path>')
async def index(request, path=""):
    return json({'hello': path})

@app.route('/other_route')
async def other_route(request, path=""):
    return json({'whatever': path})

但是,当我按下api/other_route时,我得到一个404。我知道我可以创建一个名为other_route.py的单独文件,但是我想知道是否可以在{{1} },以避免创建另一个无服务器功能。

1 个答案:

答案 0 :(得分:4)

您需要在项目根vercel.json中创建一个vercel配置

{
    "routes": [{
        "src": "/api/(.*)",
        "dest": "api/index.py"
    }]
}

这会将所有请求路由到Sanic实例/。 Sanic然后知道如何路由到处理程序。您还应该将other_route方法中的路径arg更改为path="other_route"