Forwarding a request from Node.js

huangapple go评论75阅读模式
英文:

Forwarding a request from Nodejs

问题

exports.rerouteIncomingEmail = async (req, res) => {
    var to = extractRequestHeaders(req.headers, 'x-recipient');
    var endPoint = await services.getPushEndpointFromEmailList(to, req.app.locals.db);

    endPoint ? res.redirect(308, endPoint) : res.send(200, 'endpoint not found');
};
英文:

I need to forward / redirect request received by nodejs endpoint to a .net 7 web API end point. Nodejs endpoint is triggered by an external party and it receives the request as expected. the problem is redirecting / forwarding from nodejs to .NET web api endpoint, its not getting triggered.

This is my code block.

exports.rerouteIncomingEmail = async (req, res)  => {

    var to = extractRequestHeaders(req.headers, 'x-recipient');
    var endPoint = await services.getPushEndpointFromEmailList(to,req.app.locals.db);

    endPoint ? res.redirect(308,endPoint) : res.send(200,'endpoint not found');    
};

答案1

得分: 1

你正在使用res.redirect,你需要代理请求到.NET API端点。从你的Node.js服务器创建一个新的请求到.NET API端点,然后将该请求的响应发送回原始调用方。可以使用axios等库来实现这一点。

const axios = require('axios');

exports.rerouteIncomingEmail = async (req, res) => {
    var to = extractRequestHeaders(req.headers, 'x-recipient');
    var endPoint = await services.getPushEndpointFromEmailList(to, req.app.locals.db);

    if (!endPoint) {
        res.status(200).send('endpoint not found');
        return;
    }

    try {
        // 代理请求到.NET API
        const apiResponse = await axios({
            method: req.method, // 保持相同的方法
            url: endPoint,
            headers: req.headers, // 保持相同的头部
            data: req.body, // 保持相同的请求体
        });

        // 将来自.NET API的响应发送回原始调用方
        res.status(apiResponse.status).json(apiResponse.data);
    } catch (error) {
        // 错误处理:发送500状态码和错误消息
        res.status(500).send(error.message);
    }
};

希望这个翻译对你有帮助。

英文:

You are using res.redirect, you have to proxy the request to the.NET API endpoint. Make a new request from your Node.js server to the .NET API endpoint, and then send the response from that request back to the original caller. This can be done using libraries like axios.

const axios = require('axios');

exports.rerouteIncomingEmail = async (req, res) => {
    var to = extractRequestHeaders(req.headers, 'x-recipient');
    var endPoint = await services.getPushEndpointFromEmailList(to,req.app.locals.db);
  
    if (!endPoint) {
        res.status(200).send('endpoint not found');
        return;
    }

    try {
        // Proxy the request to the .NET API
        const apiResponse = await axios({
            method: req.method, // Keep the same method
            url: endPoint,
            headers: req.headers, // Keep the same headers
            data: req.body, // Keep the same body
        });
    
        // Send back the response from the .NET API to the original caller
        res.status(apiResponse.status).json(apiResponse.data);
    } catch (error) {
        // Error handling: Send a 500 status code and the error message
        res.status(500).send(error.message);
    }
};

huangapple
  • 本文由 发表于 2023年5月21日 11:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定