首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用axios从前端调用uWebSockets http路由?

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

Stack Overflow用户
提问于 2021-04-01 23:56:35
回答 1查看 461关注 0票数 0

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

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

代码语言:javascript
复制
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);
});

在前端,我尝试像这样发送请求:

代码语言:javascript
复制
    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.04 uWebSockets.js版本。

EN

回答 1

Stack Overflow用户

发布于 2021-06-27 16:12:51

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66907856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档