简体   繁体   English

从nodejs中的aws lambda调用在本地主机上运行的api

[英]Invoke api running on localhost from aws lambda in nodejs

I am running a api on http://localhost:80/api/test , I want to invoke this api from lambda function but I am not sure what plugin or anything I should use to access same.The reason for for doing so is, I want to do testing lambda and api in development phase我在http://localhost:80/api/test上运行一个 api,我想从 lambda 函数调用这个 api 但我不确定我应该使用什么插件或任何东西来访问它。这样做的原因是, 我想在开发阶段测试 lambda 和 api

I used https://ngrok.com/ to solve it.我使用https://ngrok.com/来解决它。

Below is the command for localhost https urls.下面是localhost https urls 的命令。 You can replace the port no 3000 with your port no.您可以将端口号3000替换为您的端口号。

ngrok http https://localhost:3000 -host-header="localhost:3000"

Below is my Lambda Function:下面是我的 Lambda 函数:

var https = require('https');

exports.testJob = (event, context, callback) => {
var params = {
    host: "90abcdef.ngrok.io",
    path: "/api/test"

};

var req = https.request(params, function(res) {
    let data = '';
    console.log('STATUS: ' + res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        data += chunk;
    });
    res.on('end', function() {
        console.log("DONE");
        console.log(JSON.parse(data));
        });
    });
    req.end();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM