【发布时间】:2019-08-02 06:42:54
【问题描述】:
Netlify 函数通常位于某个子路径中,例如 /.netlify/functions。是否可以让一个函数负责渲染每个子路径,以便在函数中进行服务器端渲染?
【问题讨论】:
-
我在我的回答中展示了如何将默认函数路径作为子路径之一。响应中返回的任何有效页面都是有效的。您可以使用该方法进行测试。
标签: netlify
Netlify 函数通常位于某个子路径中,例如 /.netlify/functions。是否可以让一个函数负责渲染每个子路径,以便在函数中进行服务器端渲染?
【问题讨论】:
标签: netlify
可以在重定向文件中创建 rewrite rule 以允许函数位于更漂亮的 url 端点。
为要用作子路径的函数创建一个重写路径
_redirects(see docs here)
/hello /.netlify/functions/sayhello 200
确保/hello 路径没有有效的端点。
使用下面的sayhello 函数,您也可以传递查询参数。
sayhello.js
exports.handler = function(event, context, callback) {
const {name = 'World'} = event.queryStringParameters;
const message = `Hello to the ${name}!`
callback(null, {
statusCode: 200,
body: `${message}`
});
}
致电https://example.com/hello?name=talves
以 Hello to the talves! 作为正文响应。
【讨论】: