NodeJS route order - node.js

I'm trying to understand how the route order works in nodeJS.
I have just one route in my app.js:
const formsCreatorRoute = require('./routes/formsCreator');
app.use('/formsCreator', formsCreatorRoute);
Then, in my formsCreator.js route folder I set two routes:
const express = require('express');
const router = express.Router();
const formsCreatorController = require('../controllers/formsCreator');
router.get('/:pID/:uID', formsCreatorController.load);
router.get('/getForm/:fID/:pID', formsCreatorController.getFormByID);
module.exports = router;
The controller just export two test functions: load and getFormByID.
However, when I run http://localhost:3000/formsCreator/9/215 it should go to the load method in controller (first route), but it goes to the getFormByID. Why is it happen? Will it always run the last route?
Thanks

Related

How to structure a multiple post endpoints with node / express.js

How can I successfully configure my routes to access multiple methods on my controller accordingly to the endpoint and parameters passed to the URL?
When accessing /companies/:companyId/createCheques I would like to call the method createCheques in cheques controller but it is still calling createCheque.
I tried adding the line below to routes/cheques.js but it did not work.
router.route('/:companyId/createCheques').post(createCheques)
// routes/companies.js
const express = require('express')
const {
getCompanies,
getCompany,
deleteCompany,
createCompany,
updateCompany,
} = require('../controllers/companies')
// Include other resource routers
const userRouter = require('./users')
const chequeRouter = require('./cheques')
const redeemRouter = require('./redeems')
const router = express.Router({ mergeParams: true })
// Re-route into another resources routers
router.use('/:companyId/users', userRouter)
router.use('/:companyId/cheques', chequeRouter)
router.use('/:companyId/createCheques', chequeRouter)
router.use('/:companyId/redeems', redeemRouter)
router
.route('/')
.get(getCompanies)
.post(createCompany)
router
.route('/:id')
.get(getCompany)
.put(updateCompany)
.delete(deleteCompany)
module.exports = router;
// routes/cheques.js
const express = require('express')
const {
getCheques,
getCheque,
deleteCheque,
createCheque,
createCheques,
updateCheque
} = require('../controllers/cheques')
// when more than 1 url param is possible to the same route, mergeParams must to be set to true
const router = express.Router({ mergeParams: true })
// Advanced results
const Cheque = require('../models/Cheque')
const advancedResults = require('../middleware/advancedResults')
router
.route('/')
.get(advancedResults(Cheque, 'cheques'), getCheques)
.post(createCheque)
.post(createCheques)
router
.route('/:id')
.get(getCheque)
.put(updateCheque)
.delete(deleteCheque)
module.exports = router;
The problem you have is that you are defining two POST controllers for the exact same route.
If you want to call both controllers createCheque and createCheques, when a POST is done to :companyId/createCheques/, you only need to add next() on the last line of createCheque, just like a middleware. See https://expressjs.com/es/4x/api.html#router
If you only want to call one controller, then you need to create a separate route for the other controller on your routes/cheques.js file.
router
.route('/cheques') //here the complete path would be companyId/createCheques/cheques
.post(createCheques)

Express Router - different routes with same suffixes

I have 2 routes:
/api/system/list - list my systems
/api/system/:systemId/books/list - list books of current system
And different file for each API:
systemAPI.js:
const list = router.get('/list', Validate(Validation.list), listHandler)
return {
routes: [list]
}
bookAPI.js:
const list = router.get('/list', Validate(Validation.list), listHandler)
return {
routes: [list]
}
Finally, use the above routes:
express.use('/api/system', systemAPI.routes)
express.use('/api/system/:systemId/book', bookAPI.routes)
The problem is, when I'm entering list-books API (/api/system/:systemId/books/list), its actually calling the list-systems API (/api/system/list)
Update: Solved!
I had 2 main problems:
routes order (in app.use(..))
use different instance of Express.Router() on each API
Refer the answer below for more information.
Try reversing the order of the routes and this will probably solve your problem:
express.use('/api/system/:systemId/book', bookAPI.routes);
express.use('/api/system', systemAPI.routes);
The reason is that express evaluates routes 'greedily' and it will even resolve to partial matches. So, it is important to keep the more specific routes first before the more general ones.
Also, I think you are using the express router wrong according to the documentation the systemAPI:
const express = require('express');
const router = express.Router();
router.get('/list', Validate(Validation.list), listHandler)
module.exports = router;
The bookAPI route:
const express = require('express');
const router = express.Router();
router.get('/list', Validate(Validation.list), listHandler)
module.exports = router;
Finally import the routers and use them:
const express = require('express');
const bookRouter = require('./bookAPI'); //set the file paths based on your file structure
const systemRouter = require('./systemAPI');
const app = express();
app.use('/api/system/:systemId/book', bookRouter);
app.use('/api/system', systemRouter);

Routes in nodeJs + express

In the nodejs+express application can we have different routes files and include them into a single file. Like as below.
in hotel.routes.js I have few routes like
const express = require("express");
const hotelsCtrl = require("../controllers/hotels.controller");
const router = express.Router();
router
.route("/")
.get(hotelsCtrl.getAllHotels);
module.exports = router;
In the review.routes.js I have few routes like
const express = require("express");
const reviewsCtrl = require("../controllers/reviews.controller");
const router = express.Router();
router
.route("/")
.get(reviewsCtrl.getAllreviews);
module.exports = router;
Now I wanted to include both hotel.routes.js and review.routes.js in the index file and when the route is "/hotel" it should go to hotel.routes.js and when the route is "/review" it should go to review.routes.js
can someone please help in fixing this.
Thanks in advance.
You can do this:
app.use('/hotel', require('./hotel.routes.js'));
app.use('/review', require('./review.routes.js'));
This puts the hotel.routes.js exported router on /hotel and the review.routes.js exported router on /review.
It seems like you've a bit over complicated things by using three files in order to set up a single /hotel route.
You could also just do:
const hotelsController = require("../controllers/hotels.controller");
app.get('/hotel', hotelsController.getAllHotels);
There's really no need for the separate hotel.routes.js file.
Of, alternatively you could put the /hotel in the hotel.routes.js file like this:
const hotelsCtrl = require("../controllers/hotels.controller");
const router = require("express").Router();
router.get('/hotel', hotelsCtrl.getAllHotels);
module.exports = router;
And, then all you have to do in index.js is this:
app.use(require('./hotel.routes.js'));

Prefix all requests in JSON Server with middleware

I'm using the JSON Server package (json-server) from https://github.com/typicode/json-server. I'd like to make the server prefix all requests with /api/v2. The documentation even gives an example how to do this with the following:
server.use('/api', router)
However I don't want to setup my own server instance but extend the default when running json-server.
Can I somehow use the above statement in a middleware?
Since the router that json-server returns to you is an Express router.
First define all of your /v1, /v2, etc in its own routes file as show below:
// api-routes.js
const express = require('express')
const jsonServer = require('json-server')
const router = express.Router()
const server = jsonServer.create()
const middlewares = jsonServer.defaults()
const v1Router = jsonServer.router('db-v1.json')
const v2Router = jsonServer.router('db-v2.json')
router.use('/v1', v1Router)
router.use('/v2', v2Router)
module.exports = router
Then mount your API router onto /api like so:
const express = require('express')
const apiRoutes = require('./api-routes')
const app = express()
app.use('/api', apiRoutes)
// ...
Should now have /api/v1 and /api/v2. Untested code above, but should give an idea what you need to do.

globalize my node variable express and route

Hello guys so i have this two variables i am requiring them for every route i have so i want to globalize them and just add them to the app.js but when i do that it didn't work.
var express = require('express');
var router = express.Router();
i tried to addd them in to my separate file config.js and then i require it in my route and assign it to variable and use it but it is not working.
my config file look like this
/**
* Module dependencies.
*/
var express = require('express'),
favicon = require('serve-favicon'),
logger = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler'),
path = require('path');
module.exports = function() {
var app = express();
var router = app.Router();
return app;
}();
i get this error all the time :
throw new Error('\'app.router\' is deprecated!\nPlease see the 3.x to 4.x migration guide for details on how to update your app.');
TLDR: Your router variable is an instance of Router, and should to be initialized inside each of your route files. I can appreciate trying to be as DRY as possible, but doing it this way will not work the way you expect it to. You can save a bit of typing by writing it this way:
var router = require('express').Router();
The longer explanation is that use multiple routes, you need a router for each route. This is to help break up the whole path associated with a request (i.e. /bar/foo/baz) into more manageable chunks.
Let's say your path can start with either /cats or /dogs. In the file where you declare all your middleware, you could write:
var catRoute = require(./routes/cats)
var dogRoute = require(./routes/dogs)
app.use('/cats', catRoute)
app.use('/dogs', dogRoute)
Then, inside cats.js:
var router = require('express').Router();
router.get('/whiskers', (req, res) => {
res.send('Cat whiskers!')
}
module.exports = router;
What this does is tell the app if it gets a path starting with /cats, use the cats route, which is just another piece of middleware. Within the route itself, we don't have to specify /cats/whiskers, only /whiskers because app is only passing down the part of the path after /cats to the cats.
You can nest as many routers as you want. Check out this article for a good example of how to do that.
Hypothetically, you could use only one router

Resources