Javascript 运行node.js程序,该程序对本地计算机上运行的express服务器进行提取调用

Javascript 运行node.js程序,该程序对本地计算机上运行的express服务器进行提取调用,javascript,node.js,express,terminal,node-fetch,Javascript,Node.js,Express,Terminal,Node Fetch,如何在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

如何在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
文件,用于对该服务器进行
节点获取
调用:

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'
}
FetchError:请求https://localhost:4001/test 失败,原因:写入EPROTO
15088:错误:1408F10B:SSL例程:ssl3\u get\u记录:错误的版本号:c:\ws\dep
s\openssl\openssl\ssl\record\ssl3\u record.c:332:
在ClientRequest。(C:\Users\lowys\projects\dump\node test\node\u mo
dules\node fetch\lib\index.js:1455:11)
在ClientRequest.emit(events.js:210:5)
在TLSSocket.socketErrorListener(_http_client.js:406:9)
在TLSSocket.emit(events.js:210:5)
出错时销毁(内部/streams/destroy.js:108:12)
在onwriteError(_stream_writable.js:452:5)
在onwrite(_stream_writable.js:473:5)
在内部/streams/destroy.js:50:7
在TLSSocket.Socket.销毁(net.js:664:5)
在TLSSocket.destroy(内部/streams/destroy.js:38:8){
信息:'请求https://localhost:4001/test 失败,原因:写入EPROTO
15088:错误:1408F10B:SSL例程:ssl3\u get\u记录:错误的版本号:c:\\ws\\d
eps\\openssl\\openssl\\ssl\\record\\ssl3\u record.c:332:\n',
键入:“系统”,
errno:‘EPROTO’,
代码:“EPROTO”
}
基于协议(EPROTO)错误,尝试针对
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证书并将该证书添加到您的计算机中