struggling to figure this out. (pathing issue) #4988
itsjusttriz
started this conversation in
General
Replies: 1 comment
-
Solved!I ended up going with this: const fallback = new JSONResponse({ code: IRequest.getStatusCode('NOT_FOUND'), message: 'Fallback: Invalid Request', payload: [] });
app.use(async (req, res) => {
const dir = await fs.readdir(process.cwd() + '/dist/callbacks').catch(console.error);
if (dir) {
for (const file of dir.filter(f => f.endsWith('.js'))) {
const { endpoint } = await import(`./callbacks/${file}`);
if (endpoint?.path?.test?.(req.path)) // Tests if req.path matches a specific regex group.
await endpoint.callback(req, res);
else if (endpoint?.path === req.path) // Else, checks if req.path equals the specific path as a string.
await endpoint.callback(req, res);
else
continue;
return;
}
res.status(fallback.getCode()).send(req.query.raw === 'true' ? fallback : fallback.getMessage())
return;
}
res.end()
}); Each path of each file is either a string, or a regex group. Using - /twitch/user/:userIdOrName
+ /\/twitch\/user\/(\w+)/ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently im trying to dynamically import methods depending on request path.
I import an object from another directory, and that object contains a
path
property, e.g./user/:id
, but when i checkif (req.path !== endpoint.path) {...}
(endpoint
being the object), it NEVER matches.... I think the/:id
part is being taken literally, and not allowing substitues e.g./user/itsjusttriz
Beta Was this translation helpful? Give feedback.
All reactions