我尝试创建自己的Web界面来为我的phils色调灯创建例程。对于初学者,我试图只获取所有灯的json并通过我的快递服务器显示它
我已经尝试了不同的npm模块来进行请求,例如http got和axios,但是它们都返回错误。如果我通过邮递员提出要求,那么一切正常
应该获取数据的色调客户端
const axios = require('axios');
const bodyParser = require('body-parser');
const host = "10.0.0.xx";
const hueUsername = "xxxxxxxxxxxxxxxxxxxx";
//xx is correct data in my code
const hueBaseURL = `${host}/api/${hueUsername}`;
class hueClient{
async getAllLights(){
await axios.get(`${hueBaseURL}/lights`)
.then(function (response) {
// handle success
return response.data;
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
return "test"
});
}
}
const client = new hueClient();
exports = module.exports = client;
和我的服务器:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const hueClient = require("../heu/hueClient/hueClient");
const listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/test', (req, res) => res.send('test!'));
app.get('/hue', function (req, rsp) {
rsp.send(hueClient.getAllLights());
});
这是axios给我的错误
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80,
config:
{ url:
'10.0.0.xx/api/xxxxxxxx-CT/lights',
method: 'get',
headers:
{ Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.19.0' },
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
data: undefined },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
_events:
[Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ protocol: 'http:',
maxRedirects: 21,
maxBodyLength: 10485760,
path:
'10.0.0.xx/api/xxxxxxxxxxxx-CT/lights',
method: 'GET',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: null,
port: null,
nativeProtocols: [Object],
pathname:
'10.0.0.xx/api/xxxxxxxxxxx-CT/lights' },
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header:
'GET 10.0.0.xx/api/xxxxxxxxxxxxxxx-CT/lights HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\
nUser-Agent: axios/0.19.0\r\nHost: localhost\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path:
'10.0.0.xx/api/xxxxxxxxxxxxxxxxx-CT/lights',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] },
_currentUrl:
'http:10.0.0.xx/api/xxxxxxxxxxxxxxxxxx-CT/lights' },
response: undefined,
isAxiosError: true,
toJSON: [Function] }
``` the expected response if ofcourse the json from teh bridge