【问题标题】:302 redirect doesnt work with "azure functions express" module302 重定向不适用于“azure functions express”模块
【发布时间】:2019-10-03 23:11:27
【问题描述】:

我在 azure 函数中有 NodeJS 应用程序,我使用“azure functions express”模块。此模块中没有方法 context.done() 。并且 302 重定向每次返回带有 200 状态码的空正文。重定向适用于 context.done() 方法,例如参见Azure Functions Redirect Header

我如何实现 context.done() 或类似的东西来处理重定向。

谢谢!!!!

【问题讨论】:

    标签: node.js azure express routing azure-functions


    【解决方案1】:

    由于模块背后的想法似乎是使用 express API 本身,我相信您可以根据需要调用res.redirect(<url>)

    我已经使用以下设置对其进行了测试

    1. HttpTrigger/index.js
    const createHandler = require("azure-function-express").createHandler;
    const express = require("express");
    
    // Create express app as usual
    const app = express();
    
    app.get("/api/", (req, res) => {
      res.redirect('/api/abc/xyz');
    });
    
    app.get("/api/:foo/:bar", (req, res) => {
      res.json({
        foo: req.params.foo,
        bar: req.params.bar
      });
    });
    
    // Binds the express app to an Azure Function handler
    module.exports = createHandler(app);
    
    1. HttpTrigger/function.json
    {
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ],
          "route": "api/{*segments}"
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ]
    }
    
    1. host.json
    {
        "version":  "2.0",
        "extensions": {
          "http": {
            "routePrefix": ""
          }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 2016-10-31
      • 2015-04-16
      • 2022-10-07
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多