typescript 使用 Express、Request 和 Node.js 在打字稿中发送/处理 GET 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38493568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending/handling GET requests in typescript using Express, Request and Node.js
提问by annedroiid
I'm using a combination of Express and Request (installed using npm) to try to send a get request to get some json from the server. However no matter what I do the body that is returned is "undefined".
我正在使用 Express 和 Request(使用 npm 安装)的组合来尝试发送 get 请求以从服务器获取一些 json。但是,无论我做什么,返回的主体都是“未定义的”。
This is the code in my server.js file. The json isn't actually what I'm sending, it's just an example as I can't post what I'm actually sending.
这是我的 server.js 文件中的代码。json 实际上并不是我要发送的内容,这只是一个示例,因为我无法发布我实际发送的内容。
import express = require("express");
import bodyParser = require("body-parser");
let app = express();
app.use(bodyParser.json());
app.get('/config', function(req, res){
res.json('{name: test}');
})
app.listen(3000);
I've tried both of the following but both of them say that body is undefined.
我已经尝试了以下两种方法,但他们都说 body 是未定义的。
import request = require("request");
let req = {
url: `http://localhost:3000/config`,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
request(req, function(error, response, body){
this.config = JSON.parse(body);
})
request(`/config`, function(err, res, body) {
this.config = JSON.parse(body);
});
Does anyone know what I'm doing wrong? I've never used express or request before so any tips would be greatly appreciated.
有谁知道我做错了什么?我以前从未使用过 express 或 request,因此将不胜感激任何提示。
UPDATE
更新
If I change the request code to the following, the inside of the function is never run. Does anyone know why this would be?
如果我将请求代码更改为以下内容,则永远不会运行该函数的内部。有谁知道为什么会这样?
let req = {
url: `http://localhost:3000/config`,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
request(req, function(error, response, body){
console.log("response => "+JSON.parse(body));
return JSON.parse(body);
})
回答by Narcotics
I dont't know if you have posted whole of your server's code, it seems like you missed app.listen(port)
so that your server cannot be started up correctly.
我不知道您是否已经发布了整个服务器的代码,似乎您错过了,app.listen(port)
因此您的服务器无法正确启动。
Also, if you added if (error) { console.log(error); }
at the first line of the callback function of request
, you'll find it print an error: [Error: Invalid URI "/config"]
此外,如果您if (error) { console.log(error); }
在 的回调函数的第一行添加request
,您会发现它打印错误:[Error: Invalid URI "/config"]
And that's why the body
is always undefined
: You have to give the full url such like http://localhost:xxxx
to request
.
这就是为什么body
总是undefined
:您必须提供完整的网址,例如http://localhost:xxxx
to request
。
In short:
简而言之:
- Your server didn't listen to a specific port.
app.listen(5678)
- Your client didn't know the complete url.
request('http://localhost:5678/config', (...)=>{...})
- 您的服务器没有侦听特定端口。
app.listen(5678)
- 您的客户不知道完整的网址。
request('http://localhost:5678/config', (...)=>{...})
回答by Samuel Toh
Since OP hasn't got it working and I believe the code he got up there is correct. I may as well post my working solution here to help him get started.
由于 OP 还没有让它工作,我相信他在那里得到的代码是正确的。我不妨在这里发布我的工作解决方案以帮助他入门。
Hopefully this will save you hours of debugging...
希望这可以为您节省数小时的调试时间...
Client:
客户:
"use strict";
let request = require("request");
let req = {
url: `localhost:4444/config`,
proxy: 'http://localhost:4444',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
request(req, function (err, res, body) {
this.config = JSON.parse(body);
console.log("response => " + this.config);
});
Server:
服务器:
"use strict";
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var config = require('config');
app.use(bodyParser.json());
app.get('/config', function(req, res){
res.json('{name: test}');
});
// Start the server
app.set('port', 4444);
app.listen(app.get('port'), "0.0.0.0", function() {
console.log('started');
});
Output:
输出:
response => {name: test}
响应 => {名称:测试}