gpt4 book ai didi

javascript - 使用 Express 在单独的 Node 模块中执行 POST 请求

转载 作者:行者123 更新时间:2023-12-03 06:44:19 25 4
gpt4 key购买 nike

我正在构建一个 Web 应用程序,该应用程序处理 POST 请求,然后向另一台服务器执行 POST 请求,然后根据返回的信息重定向用户。

最终结果是用户输入用户名并单击提交 --> 应用程序处理帖子,获取用户名 --> 应用程序将帖子发送到外部服务器,包括用户名 --> 服务器返回用户应访问的服务器的 url be on --> 应用程序将用户重定向到该应用程序。

服务器.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var findUser = require('./findUserInstance')

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(express.static('public'));

app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})

app.post('/process_post', urlencodedParser, function (req, res) {

// Prepare output in JSON format
response = {
username:req.body.username
};
var uUrl = findUser.url(response.username);
console.log("redirecting to " + uUrl);
res.redirect(findUser.url(response.username));
res.end(JSON.stringify(response));
})

var server = app.listen(8081, function () {

var host = server.address().address
var port = server.address().port

console.log("App listening at http://%s:%s", host, port)

})

findUserInstance.js

exports.url = function(uName) {

var http = require("https");
var uUrl;

var options = {
"method": "POST",
"hostname": "removed",
"port": null,
"path": "removed",
"headers": {
"appkey": "removed",
"content-type": "application/json",
"cache-control": "no-cache",
"Accept": "application/json",
"postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd"
}
};

var req = http.request(options, function (res) {
var chunks = [];

res.on("data", function (chunk) {
chunks.push(chunk);
});

res.on("end", function () {
var body = Buffer.concat(chunks);
var jsoncontent = JSON.parse(body);
uUrl = jsoncontent.rows[0].url;
console.log("The following should be: user.instance.url.com)
console.log(jsoncontent.rows[0].url);
return uUrl; //The information that I want to return to server.js
});
});

req.write(JSON.stringify({username: uName}));
req.end();
}

问题在于将信息从外部 post 模块返回到 server.js 模块,以便它可以执行重定向。目前,我有从函数返回的变量 uUrl(已正确填充帖子中的 URL)。然而 findUserInstance 模块返回 null。

如何从findUserInstance模块获取uUrl的值到server.js模块?

最佳答案

@bryan euton 很好的回应,你应该像 Promise 一样返回 findUserInstance 中的任何对象! https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise

exports.url = function(uName) {
return new Promise( function (resolve, reject){

var http = require("https");
var uUrl;

var options = {
"method": "POST",
"hostname": "removed",
"port": null,
"path": "removed",
"headers": {
"appkey": "removed",
"content-type": "application/json",
"cache-control": "no-cache",
"Accept": "application/json",
"postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd"
}
};

var req = http.request(options, function (res) {
var chunks = [];

res.on("data", function (chunk) {
chunks.push(chunk);
});

res.on("end", function () {
var body = Buffer.concat(chunks);
var jsoncontent = JSON.parse(body);
uUrl = jsoncontent.rows[0].url;
console.log("The following should be: user.instance.url.com)
console.log(jsoncontent.rows[0].url);
resolve(uUrl); //The information resolve promise with your datas
});

});

req.write(JSON.stringify({username: uName}));
req.end();

});

}

关于javascript - 使用 Express 在单独的 Node 模块中执行 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37812859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com