Does uWebSockets.js support http.request, or a node-style fetch? #390
-
I've tried making an http request to a private http authentication server during the
installed and have tried making requests using both |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
I just tried node http and it works fine, you are probably doing something wrong, post your code here. I use this bent library for all my requests, it is newish based on promises. Did you try newer Node version, v10 is old they at 15 now const getJson=require('bent')('json')
const obj=await getJson(url) |
Beta Was this translation helpful? Give feedback.
-
No this is not correct. You are doing something else - most probably passing wrong types or wrong number of arguments to functions with relaxed type checking. How does your code look like? |
Beta Was this translation helpful? Give feedback.
-
@alexhultman Here's the short snippet: import fetch from "node-fetch";
import * as http from "http";
export class UWebSocketsTracker {
private async isAuthenticated(request: HttpRequest): Promise<boolean> {
const headers = {
"X-Auth": "some-val"
};
const url = "http://localhost:9000";
// request code
}
} where the try {
const resp = await fetch(url, headers);
console.log(resp);
return resp.ok;
} catch (e) {
console.log("exception", e);
return false;
} or return fetch(url, headers).then(resp => {
return resp.ok;
}).catch(e => {
console.log(e);
return false;
}) private readonly onUpgrade = (
res: HttpResponse, req: HttpRequest, context: us_socket_context_t
): void => {
const url = req.getUrl();
const secWebSocketKey = req.getHeader('sec-websocket-key');
const secWebSocketProtocol = req.getHeader('sec-websocket-protocol');
const secWebSocketExtensions = req.getHeader('sec-websocket-extensions');
this.isAuthenticated(req).then(isAuthenticated => {
if (isAuthenticated) {
res.upgrade(
{ url: url },
secWebSocketKey,
secWebSocketProtocol,
secWebSocketExtensions,
context
);
} else {
throw new Error("not authenticate");
}
}).catch(e => {
res.writeStatus("400 Not Authorized").end("not authenticated");
})
} |
Beta Was this translation helpful? Give feedback.
-
Well I want to see your use of uWebSockets.js, not some entirely irrelevant 😉 |
Beta Was this translation helpful? Give feedback.
-
@boulderwebdev simplify to post full file of an example that throws error, so can see what you are doing wrong. Also try updating to uWS.js v18.10 which includes some type checking, it would print out error for you @alexhultman you need to update Readme for new versions, it still says latest install is v18.8 which is what he using |
Beta Was this translation helpful? Give feedback.
-
@hst-m Let me write up an example real quick @alexhultman I updated the code above, but will create a test case so we can all see what's exactly going on. Also, here's the full error message Error: Returning from a request handler without responding or attaching an abort handler is forbidden!
terminate called without an active exception
Aborted (core dumped) |
Beta Was this translation helpful? Give feedback.
-
Okay so there you have it |
Beta Was this translation helpful? Give feedback.
-
@boulderwebdev Why did you not include that in issue? that is your problem, you need to respond to the request or attach onAborted handler before doing any async, see example |
Beta Was this translation helpful? Give feedback.
-
Ah, thanks for the pointer @hst-m |
Beta Was this translation helpful? Give feedback.
-
It is helpful to post the full error message if you are going to create an issue for this error :P |
Beta Was this translation helpful? Give feedback.
@boulderwebdev Why did you not include that in issue? that is your problem, you need to respond to the request or attach onAborted handler before doing any async, see example