ExpressJS: Adding routes dynamically at runtime

I want to be able to add new routes at runtime without restarting the server with NodeJS & ExpressJS. I made a similiar approach like in this article: https://alexanderzeitler.com/articles/expressjs-dynamic-runtime-routing/Technically I’m able to add new files and logic at runtime likewise in the article, but the problem is that when no api route was matched I’ll send a 404 JSON respond (as it is supposed to be). I think the problem that I’m having is that my dynamically created routes are never reached, because static routes have priority over dynamically created routes. This means that the created routes will be mounted

req is undefined when using req.accept inside nested function

I’ve recently come across a problem when working with the built-in req.accepts, req.acceptsLanguages, req.acceptsCharsets, and req.acceptsEncodings functions in express. I have an express middleware function like this: Which, in theory, should work. But the program keeps complaining and logs this error: The thing is, when I use req.accepts or one of those .accepts functions in the main function (acceptCheckpoint), like this: It works. And, when I log the req object in either of those functions, it returns the expected value. I’ve also tried logging the req object in the request.js file of the express module, and there, it returned undefined.

Node.js response API vs. Express.js response API

Both, Node.js and Express.js have response API. And it looks like there is even some overlapping between them, e.g.: Node.js: response.write(chunk[, encoding][, callback]) Express.js: res.attachment([filename]) res.download(path [, filename] [, options] [, fn]) res.sendFile(path [, options] [, fn]) res.send([body]) Which response API should be used for sending a response (e.g. JSON, files)? In other words, should I use response.write by Node.js or res.sendFile by Express.js? P.S. Initially for some reason I assumed that Express.js is now a part of Node.js, which is not a right assumption, of course. Answer Bearing in mind that Express.js is now a part of Node.js It

How to set Heroku Port for NodeJS Express App?

so I’m trying to host my website on Heroku and set up everything to get my app up and running. Whenever I try to submit the form I get undefined errors. Undefined Errors Console Errors I’ve set it up to use the port like shown in the documenation: When starting the app locally with heroku local web I get Typerror: Failed to Fetch and the undefined results but when I go into my .env file and add a port=8081 it works perfectly fine. Good result When I open it with heroku open I still have that undefined problem. I don’t

Express POST API Route not receiving a Supertest request

I am trying to test an Express API POST Route that uses Express Validator for check: This API route expects to receive a request consisting of a body that contains the fields, name, email, and password: In order to test this route, I have a test file using supertest and jest: When I execute this test, each check in the POST API route fails. The following errors is returned: Why is the API route not receiving the request I’m sending using Supertest? Answer Well, it seems that you’re not sending your values right. Look closely at where you send your

ExpressJS: Adding routes dynamically at runtime

I want to be able to add new routes at runtime without restarting the server with NodeJS & ExpressJS. I made a similiar approach like in this article: https://alexanderzeitler.com/articles/expressjs-dynamic-runtime-routing/Technically I’m able to add new files and logic at runtime likewise in the article, but the problem is that when no api route was matched I’ll send a 404 JSON respond (as it is supposed to be). I think the problem that I’m having is that my dynamically created routes are never reached, because static routes have priority over dynamically created routes. This means that the created routes will be mounted

extend existing API with custom endpoints

I’m creating an API for multiple customers. The core endpoints like /users are used by every customer but some endpoints rely on individual customization. So it might be that User A wants a special endpoint /groups and no other customer will have that feature. Just as a sidenote, each customer would also use his own database schema because of those extra features. I personally use NestJs (Express under the hood). So the app.module currently registers all my core modules (with their own endpoints etc.) I think this problem is not related to NestJs so how would you handle that in

res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express

I have tried and and I have tried to set the cookie outright with but no cookie is set when I examine the res, and no cookie is received in my frontend. Everything is run locally. I do not understand why. Answer I found the answer. When using fetch() from the front end I didn’t set the credentials flag to include. When I included the flag it worked instantly.

express.js – how to intercept response.send() / response.json()

Lets say I have multiple places where I call response.send(someData). Now I want to create a single global interceptor where I catch all .send methods and make some changes to someData. Is there any way in express.js? (hooks, listeners, interceptors, …)? Answer You can define a middleware as below (taken and modified from this answer)

How to generate unique ID with node.js

How to set a variable value with database query callback? How I can do it? Answer It’s been some time since I used node.js, but I think I might be able to help. Firstly, in node, you only have a single thread and are supposed to use callbacks. What will happen with your code, is that base.getID query will get queued up by for execution, but the while loop will continusouly run as a busy loop pointlessly. You should be able to solve your issue with a callback as follows: And use it as such I haven’t coded any node/js