catch plural route in expressjs - node.js

in expressjs, I use routing like below;
app.use('/game', require("./routes/game"));
in the file /routes/game.js
const express = require('express');
var router = express.Router();
router.get("s", function (req, res, next) {
res.send("GAME LIST");
})
router.get("/:gameurl", function (req, res, next) {
res.send(`GAME: ${req.params.gameurl}`);
})
module.exports = router;
I'd like to catch both /games and /game/wow
How can I manage to handle both routes separately?

If you want both /games and /game to go to your router, but not any other top level paths, then there are a number of ways to specify it. You can see them described here in the doc. For example, you could use a regex or pass multiple strings. In this case, I'll show you the multiple strings:
app.use(["/game", "/games"], require("./routes/game"));
For the route path, you can pass a single string, a path pattern (an Express subset of regex), a regex, or an array that contains any combination of these.
If you want to be able to tell the difference between /game and /games in your router, then you will have to examine req.originalUrl to see which one caused it to go to your router which seems to me to kind of defeat part of the purpose of routing in the first place.
Thus, sending two separate top level paths to the same router and routing them differently inside the router is not a design that works well with Express. Personally, I'd either change my path design so this doesn't happen or use two routers as that fits better with the router mechanics.

you can do something like this
app.use('/', require("./routes/game"));
in the file /routes/game.js
const express = require('express');
var router = express.Router();
const base = '/game';
router.get(`${base}s`, function (req, res, next) {
res.send("GAME LIST");
})
router.get(`${base}/:gameurl`, function (req, res, next) {
res.send(`GAME: ${req.params.gameurl}`);
})
module.exports = router;

Related

Express subroute router, without having to use full path in each route

I'm splitting out part of the overall API as a separate subroute, i.e. (code simplified for this question)
const mainRouter = express.Router();
const bananaRouter = express.Router();
const appleRouter = express.Router();
bananaRouter.get('/banana/hello', (req, res) => { /* ... */ });
mainRouter.all('/banana/*', bananaRouter);
mainRouter.all('/apple/*', appleRouter);
This works. But I don't want to specify the full '/banana/hello' in every single route, and would much prefer '/hello'.
How can I do that?
Use .use() with a path prefix (no wildcard) instead of .all(). .use() will automatically match a path prefix without a wildcard whereas .get() and .all() will not and this will then set up your router better. So, do this to register the router:
// send all /banana prefixed urls to the bananaRouter
mainRouter.use('/banana', bananaRouter);
Then, inside of that router, you don't use the /banana part of the path:
// will match /banana/hello
bananaRouter.get('/hello', (req, res) => { /* ... */ });
In general, you will use .use() for routers and other middleware, not .all().

Express - can't import routes

I have a following structure,
// app.js
const express = require("express");
const app = express();
app.get("/", require("./routes/index"));
app.get("/users", require("./routes/users"));
app.listen(3000);
// /routes/index.js
const express = require("express");
const router = express.Router();
router.get("/", (req, res) => res.send("index"));
module.exports = router;
// /routes/users.js
const express = require("express");
const router = express.Router();
router.get("/login", (req, res) => res.send("login"));
router.get("/register", (req, res) => res.send("register"));
module.exports = router;
If I use app.use(...) inside app.js then the routes work correctly but I want to use app.get since I want to block any other accessible method.
Right now the index route works fine but other routes does not work.
Working sandbox: https://codesandbox.io/s/cocky-cartwright-h82d2?fontsize=14&hidenavigation=1&theme=dark
The correct way to achieve what you're trying is to utilize the 'use' function.
You said you don't want to use the app.use because you want to block any other accessible method, but utilizing the 'use' function won't allow anything that wasn't declared on your routers, so you don't need to worry about that.
What app.use does is register a middleware function to your app on the specified route. So, the code below:
app.use("/users", require("./routes/users"));
Will make that every request that match the pattern '/users' will utilize the function you provided (in this case, the router inside the users.js file).
So, if someone sends a POST request to /users/register, per example, he will get a 404, because you never created a route to handle a post on that path.
And just to make it clear why the app.get don't work the way you did, let me give a brief explanation.
When you use the app.get function, it'll be expected that the request path matches exactly the one you provided. So, when you do the following:
app.get("/users", require("./routes/users"));
The server will be expecting a request that matches /users exactly. So, something like /users/example will not trigger the callback function.
The thing is, the function to handle the request in the code above is another router:
router.get("/login", (req, res) => res.send("login"));
router.get("/register", (req, res) => res.send("register"));
So, the router will expect that the request path matches /users/login or /users/register exactly. But, if a request path matches /users/register, it will not match /users, so nothing will be called.
The reason why your '/' path works right now is because your router is expecting the same path you used on the app.js file. So, a request to '/' will match both patterns.
you can't use router.get and pass a router
router.get expect to get a method (function).
you should use app.use or use controllers like that
// /controllers/index.js
exports.renderIndex = (req, res) => {
res.send("index")
}
and in app.js
// app.js
const express = require("express");
const app = express();
const indexController = require("./controllers/index");
app.get("/", indexController.renderIndex);
app.listen(3000);

How to split a routes.js that grows too large to be easily maintained?

I'm using node and express to create a rest api. I followed a tutorial where all the routes and its logic are saved in a routes.js file like this:
SERVER JS:
var express = require('express');
var app = express();
(...)
require('./app/routes.js')(app, port, express);
ROUTES.JS
module.exports = function(app, port, express) {
var apiRoutes = express.Router();
(...)
//Sample route
apiRoutes.get('/userfiles', function(req, res) {
UserFile.find({ owner: req.decoded.user.email }, function(err, filesList) {
if (err)
return done(err);
res.json({ success: true, files: filesList });
});
});
My problem is twofold:
1 - Routes can easily contain code thats 150 lines long, some of them far longer. It doesn't feel clean to have route declarations and the logic grouped together. Is it a good practice to do something like this instead?
apiRoutes.post('/randomRoute', function(req, res) {
return res.json(functionThatContainsTheActualCode(req));
});
(and then have an functionThatContainsTheActualCode function with all the logic in a different file).
2 - I have middleware that applies to some functions (for example, some routes are only accessible for logged in users and those routes go through an authentication middleware). Currently way I do it is declaring public routes before the middleware declaration and private routes after, which feels incredibly hacky. How can I separate public and private routes (and the middleware itself) in different files?
Problem 1:
We need to go deeper.
Change the route file to just require the actual router logic.
routes.js
// where app = express();
module.exports = (app) => {
// index.js happens to be a file exporting the router.
app.use('/', require('./index'));
// this is basically the idea. Create a separate file for the actual logic.
app.use('/route', require('.path/to/file'));
};
and in file.js
const express = require('express'),
router = express.Router();
router.verb('/path/', (req, res, next) => {
// do whatever
});
// this is required
module.exports = router;
Problem 2:
Middleware is basically a function taking in request, response, next as 3 params, doing something with the request and either sending out a response or moving on to the next middleware. That's why you need to call next if you want to move to next middleware in the chain.
Now all you need is a file that exports a function which takes request, response, next as params.
// lets call this auth.js
module.exports = function(req, res, next) {
// do logic
if () {
return res.send(); // or res.somethingThatSendsOutAHttpResponse()
}
// next middelware
next();
};
Since express routes are also middlewares, (mind blown), you can mount them top down.
To authenticate a route, just put the auth.js middleware on top of that route.
router.get('/', require('./auth'));
router.get('/', require('./log'));
router.get('/', (req, res, next) => {
// yolo
});
Now since this is web dev, you still got problems.
Now all your boring database queries are scattered everywhere.
Fear not, you can solve it, by, guess, creating another file.
apiRoutes.get('/userfiles', function(req, res) {
const userFile = require('/path/to/model/with/userfile/methods/exported/out');
// do something with userFile's methods
});

Node express routes as modules: where to put `require`s?

Node Express's Routing guide gives the following example for creating routes as modules:
/birds.js:
var express = require('express')
var router = express.Router()
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About birds')
})
module.exports = router
/app.js:
var birds = require('./birds')
// ...
app.use('/birds', birds)
I want to know why they put the first two lines of birds.js there instead of in app.js.
Firstly, app.js calls a method of app. How is app supposed to be defined within app.js? I take it they (oddly) neglected to include that necessary code for the sake of the tutorial.
Secondly, say I wanted a second route as a module, for dogs as well as birds, in a file called dogs.js. Could it look identical to birds.js WRT the first two lines? AFAIK that would result in two instances of express. (Or three if it's needed in app.js as well?!)
The example is not complete. The whole app setup is left out (i asume because it is explained further up in the docs anyway and replaced with // ...). In your app.js you need at least:
var express = require('express');
var app = express();
The first 2 lines in bird.js have nothing to do with the two (missing) lines in app.js. You need them to create a router.
And regarding your last question: Yes, you would create another router exactly like the bird router. A router is not an express app/instance and it's totally fine to have multiple of them in you app.

Node.js Express nested resources

i'd like to nest resources like the below eg.
/// fileA.js
app.use('/dashboards/:dashboard_id/teams', teams);
[...]
///teams.js
router.get('/', function(req, res, next) {
[...]
}
but I can't get the req.params["dashboard_id"] parameter in teams because it seems it is already substituted with the parameter value.
I've tried to baypass the problem by calling an intermediate function and pass somewhere the parameter but I can't figure out where ...
Do you have an answer?
Thanks,
Franco
You may try this solution:
//declare a function that will pass primary router's params to the request
var passPrimaryParams = function(req, res, next) {
req.primaryParams = req.params;
next();
}
/// fileA.js
app.use('/dashboards/:dashboard_id/teams', passPrimaryParams);
app.use('/dashboards/:dashboard_id/teams', teams);
///teams.js
router.get('/', function(req, res, next) {
var dashboardId = req.primaryParams['dashboard_id']; //should work now
//here you may also use req.params -- current router's params
}
Using Express 4.0.0 or above at the time of this writing:
To make the router understand nested resources with variables you will need create and bind a new router using app.use for every base path.
//creates a new router
var dashboardRouter = express.router();
//bind your route
dashboardRouter.get("/:dashboard_id/teams", teams);
//bind to application router
app.use('/dashboards', dashboardRouter);
This way Express will see the first part of the path and go to the /dashboards route, which has the :dashboard_id/teams path.
You can use the mergeParams option here
// fileA.js
app.use('/dashboards/:dashboard_id/teams', teams);
// teams.js
const router = express.Router({ mergeParams: true })
router.get('/', function(req, res, next) {
// you will have access to req.params.dashboard_id here
}
You can also see this answer: Rest with Express.js nested router

Resources