【发布时间】:2020-10-22 06:08:07
【问题描述】:
我有 2 个 node.js 服务器正在运行; 1 在端口 8000 另一个在 8080 端口
8080端口是API,所以需要向它发送POST请求。(端点:websocket/test)。 当我尝试这样做时,会返回 404。 它位于一个子目录(ROOT/webhook)中,所以不确定是不是这个原因,或者它是否在端口 8080 上?
Socket.io 工作正常,连接没有问题,我只是无法向该服务器发送 POST 请求。
这里是 server.js 文件:
//SOCKET.IO Server
const express = require('express');
const app = express();
const port = 8080;
const fs = require('fs');
const http = require('http');
const https = require('https');
const sslKey = 'HIDDEN';
const sslCert = 'HIDDEN';
const options = {
key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert)
};
const httpServer = http.createServer();
const httpsServer = https.createServer(options);
const io = require('socket.io')(httpsServer);
// FOR HTTP
// httpServer.listen(port, () => {
// console.log("Socket.io http server is listening on port: " + port)
// console.log(__dirname + '/key.pem');
// })
// FOR HTTPS
httpsServer.listen(port, () => {
console.log("Socket.io https server is listening on port: " + port);
})
io.on('connection', function(socket){
console.log('made socket connection', socket.id);
socket.emit('welcome', 'Hiya! Welcome');
app.post('/websocket/test', function() {
console.log('webhook received');
io.emit('webhook', 'You have received a webhook!');
});
});
【问题讨论】: