运行 一个 node.js 程序,它在我的本地计算机上对快速服务器 运行 进行提取调用
Running a node.js program that makes a fetch call to an express server running on my local machine
我如何 运行 node.js 中的 javascript 程序对本地机器上的服务器 运行 进行本地获取调用?
我有一个本地 express
服务器正在侦听端口 4001:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4001;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
})
app.use('/test', (req, res, next) => {
res.send('test');
})
我有一个单独的 app.js
文件,可以 node-fetch
调用此服务器:
const fetch = require('node-fetch');
const payload = fetch('https://localhost:4001/test');
payload.then(response => response.json())
.then(jsonResponse => {
console.log(jsonResponse);
}).catch(error => {
console.log(error);
})
运行 node app.js
在终端中导致以下错误消息:
FetchError: request to https://localhost:4001/test failed, reason: write EPROTO
15088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\dep
s\openssl\openssl\ssl\record\ssl3_record.c:332:
at ClientRequest.<anonymous> (C:\Users\lowys\projects\dump\node-test\node_mo
dules\node-fetch\lib\index.js:1455:11)
at ClientRequest.emit (events.js:210:5)
at TLSSocket.socketErrorListener (_http_client.js:406:9)
at TLSSocket.emit (events.js:210:5)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:452:5)
at onwrite (_stream_writable.js:473:5)
at internal/streams/destroy.js:50:7
at TLSSocket.Socket._destroy (net.js:664:5)
at TLSSocket.destroy (internal/streams/destroy.js:38:8) {
message: 'request to https://localhost:4001/test failed, reason: write EPROTO
15088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\d
eps\openssl\openssl\ssl\record\ssl3_record.c:332:\n',
type: 'system',
errno: 'EPROTO',
code: 'EPROTO'
}
根据协议 (EPROTO) 错误,考虑到其 localhost
:
,请尝试将 http
而不是 https
作为目标
const fetch = require('node-fetch');
const payload = fetch('http://localhost:4001/test');
payload.then(response => response.json())
.then(jsonResponse => {
console.log(jsonResponse);
}).catch(error => {
console.log(error);
})
除非您创建了某种根 SSL 证书并将该证书添加到您的计算机。
我如何 运行 node.js 中的 javascript 程序对本地机器上的服务器 运行 进行本地获取调用?
我有一个本地 express
服务器正在侦听端口 4001:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4001;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
})
app.use('/test', (req, res, next) => {
res.send('test');
})
我有一个单独的 app.js
文件,可以 node-fetch
调用此服务器:
const fetch = require('node-fetch');
const payload = fetch('https://localhost:4001/test');
payload.then(response => response.json())
.then(jsonResponse => {
console.log(jsonResponse);
}).catch(error => {
console.log(error);
})
运行 node app.js
在终端中导致以下错误消息:
FetchError: request to https://localhost:4001/test failed, reason: write EPROTO
15088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\dep
s\openssl\openssl\ssl\record\ssl3_record.c:332:
at ClientRequest.<anonymous> (C:\Users\lowys\projects\dump\node-test\node_mo
dules\node-fetch\lib\index.js:1455:11)
at ClientRequest.emit (events.js:210:5)
at TLSSocket.socketErrorListener (_http_client.js:406:9)
at TLSSocket.emit (events.js:210:5)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:452:5)
at onwrite (_stream_writable.js:473:5)
at internal/streams/destroy.js:50:7
at TLSSocket.Socket._destroy (net.js:664:5)
at TLSSocket.destroy (internal/streams/destroy.js:38:8) {
message: 'request to https://localhost:4001/test failed, reason: write EPROTO
15088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\d
eps\openssl\openssl\ssl\record\ssl3_record.c:332:\n',
type: 'system',
errno: 'EPROTO',
code: 'EPROTO'
}
根据协议 (EPROTO) 错误,考虑到其 localhost
:
http
而不是 https
作为目标
const fetch = require('node-fetch');
const payload = fetch('http://localhost:4001/test');
payload.then(response => response.json())
.then(jsonResponse => {
console.log(jsonResponse);
}).catch(error => {
console.log(error);
})
除非您创建了某种根 SSL 证书并将该证书添加到您的计算机。