如何使用axios从前端调用uWebSockets http路由



我在nodejs应用程序上使用uWebSockets,并试图向wss服务器发送一些http请求。通过从浏览器url栏访问GET路由,我可以在wss服务器上看到它们的输出,但当我尝试调用PUT、POST、PATCH。。。路由我没有得到输出(例如:没有响应,我认为没有到达路由(。

我的uWebSockets.js配置是以下代码片段:

const uWS = require('uWebSockets.js');
const SOCKET_PORT = 3023;
const websockets = uWS.App()
/* I tried to put these routes before and after .ws() , still no response */
.put('/route1', (req, res) => {
console.log('/route1');
})
.patch('/route2', (req, res) => {
console.log('/route2');
})
.post('/route3', (req, res) => {
console.log('/route3');
})
.delete('/route4', (req, res) => {
console.log('/route4');
})
/* Tried with '' , '*' , '/*' but none of the attempts succeeed */
.ws('', {
compression: uWS.SHARED_COMPRESSOR,
idleTimeout: 30,
maxBackpressure: 1024,
maxPayloadLength: 512,
open:ws=> ws.subscribe('all')
});

websockets.listen(SOCKET_PORT, (listenSocket) => {
if (listenSocket) 
console.log('Listening to port ' + SOCKET_PORT);
});

从前端,我尝试发送这样的请求:

let header = { "Access-Control-Allow-Origin" : "*" , "Content-Type" : "application/json" };
header.key= "test_key";
await axios({
method : "PUT",
url : "wss://subdomain.domain.xyz/route1",
data : { "val1" : "test" , "val2" : 5 },
headers : header
})
.then(function(response){
serverResponse = response.data;
})
.catch(function(err){
serverResponse = err.response.data;
});

我是不是做错了websockets的请求?

我使用的是18.04uWebSockets.js版本。

当使用GET、POST、PATCH、PUT、DELETE等http方法时,您应该使用http/https而不是ws/ws进行连接。

最新更新