0
我工作的一個簡單的「應用程序」,因而,試圖從一個API(OMDB)檢索信息,但不斷收到以下錯誤消息:使用Cloud 9的Node/Express API請求代理錯誤。我該如何解決?
express deprecated res.send(status, body): Use res.status(status).send(body)
instead app.js:18:10
_http_server.js:193
throw new RangeError(`Invalid status code: ${statusCode}`);
^
RangeError: Invalid status code: 0
at ServerResponse.writeHead (_http_server.js:193:11)
at ServerResponse._implicitHeader (_http_server.js:158:8)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:585:10)
at ServerResponse.send (/home/ubuntu/workspace/APIs/movieapp/node_modules/express/lib/response.js:211:10)
at ServerResponse.json (/home/ubuntu/workspace/APIs/movieapp/node_modules/express/lib/response.js:256:15)
at ServerResponse.send (/home/ubuntu/workspace/APIs/movieapp/node_modules/express/lib/response.js:158:21)
at Request._callback (/home/ubuntu/workspace/APIs/movieapp/app.js:18:10)
at Request.self.callback (/home/ubuntu/workspace/APIs/node_modules/request/request.js:187:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
我在雲9,和我只是因爲NodeJS/Express的問題,我的腳溼了,所以我在這裏有點迷路。我試着按照幾個建議編輯代碼/查看類似的查詢,但似乎沒有任何工作。我在網頁上收到的消息錯誤消息如下:ECONNRESET:無法代理請求!代理請求時發生錯誤。
這裏是我的代碼:
var express = require("express");
var app = express();
var request = require("request");
app.get("/", function(req, res) {
res.send("Welcome to Our Site");
});
app.get("/movie", function(req, res) {
request('http://www.omdbapi.com/?t=Inception', function (error, response, body) {
if (error) {
res.send('error:', error);
}
else if (Number(response.statusCode) !== 200) {
res.send('statusCode:', response && response.statusCode);
}
else {
res.send('Awards:', JSON.parse(body).Awards);
}
});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Server started!!!");
});
附註:我試圖使下面的請求和它的作品:
var request = require('request');
request('http://www.omdbapi.com/?t=Inception', function (error, response, body) {
if (error) {
console.log('error:', error);
}
else if (Number(response.statusCode) !== 200) {
console.log('statusCode:', response && response.statusCode);
}
else {
console.log('Awards:', JSON.parse(body).Awards);
}
});
謝謝!我對此失去了頭腦。 –