Express routing GET with search params - node.js

I have two GET routes for get stores but, one route is for get all stores and the other route is for get just nearby stores.
1) The url request for get all stores is as follows:
http://mydomain/stores
2) The url for get all nearby stores:
http://mydomain/stores?lat={lat}&lng={lng}&radius={radius}
The question is:
How can I map those urls properly in Express, in a way to redirect each route to the corresponding method?
app.get('/stores', store.getAll);
app.get('/stores', store.getNear);

app.get('/stores', function(req, res, next){
if(req.query['lat'] && req.query['lng'] && req.query['radius']){
store.getNear(req, res, next);
} else {
store.getAll(req, res, next)
};
});
edit - a second way to do it:
store.getNear = function(req, res, next){
if(req.query['lat'] && req.query['lng'] && req.query['radius']){
// do whatever it is you usually do in getNear
} else { // proceed to the next matching routing function
next()
};
}
store.getAll = function(req, res, next){
// do whatever you usually do in getAll
}
app.get('/stores', store.getNear, store.getAll)
// equivalent:
// app.get('/stores', store.getNear)
// app.get('/stores', store.getAll)

Related

NODEJS - Reroute on same Url

I am kind of stuck in something. Please help me
The problem i want to solve is -
I am getting some post parameters and i want to route based on those parameters in NodeJs. Now the issue is when i use switch case to route on base of the post params rerouting is not happening.
router.post('/', function (req, res, next) {
var method = req.body.method;
switch (method) {
case 'register_user':
router.post('/', userController.registerUser);
break;
case 'user_login':
router.post('/', userController);
break;
}
});
Your rerouting code inside switch context just appends more middlewares on the path /, not actually do the route as you think.
Revise like this:
router.post('/', function (req, res, next) {
var method = req.body.method;
switch (method) {
case 'register_user':
// returned since you want route to the function
return userController.registerUser(req, res, next);
break;
case 'user_login':
return userController(req, res, next);
break;
}
});

How to delegate route processing in express?

I have expressjs application with straitfort route processing like the following:
app.route('/').get(function(req, res, next) {
// handling code;
});
app.route('/account').get(function(req, res, next) {
// handling code;
});
app.route('/profile').get(function(req, res, next) {
// handling code;
});
At now I put all my code inside route handler but I want try to delegate it to some class such as the following.
app.route('/').get(function(req, res, next) {
new IndexPageController().get(req, res, next);
});
app.route('/account').get(function(req, res, next) {
new AccountPageController().get(req, res, next);
});
app.route('/profile').get(function(req, res, next) {
new ProfilePageController().get(req, res, next);
});
So what do you thing about the approach above and meybe you know the better one?
As you can see in the Express Response documentation - the response (req) can send information to the client by a few methods. The easiest way is to use req.render like:
// send the rendered view to the client
res.render('index');
Knowing this means that you can do whatever you want in another function, and at the end just call res.render (or any other method that sends information to the client). For example:
app.route('/').get(function(req, res, next) {
ndexPageController().get(req, res, next);
});
// in your IndexPageController:
function IndexPageController() {
function get(req, res, next) {
doSomeDatabaseCall(function(results) {
res.render('page', results);
}
}
return {
get: get
}
}
// actually instantiate it here and so module.exports
// will be equal to { get: get } with a reference to the real get method
// this way each time you require('IndexPageController') you won't
// create new instance, but rather user the already created one
// and just call a method on it.
module.exports = new IndexPageController();
There isn't strict approach to this. You can pass the response and someone else call render. Or you can wait for another thing to happen (like db call) and then call render. Everything is up to you - you just need to somehow send information to the client :)

forwarding to another route handler without redirecting in express

I have the following code :
app.get('/payment', function(req, res) {
// do lots of stuff
});
now I want to add the following :
app.post('/payment', function(req, res) {
req.myvar = 'put something here';
// now do the same as app.get() above
});
Obviously I want to reuse the code. I tried doing next('/payment') inside the post handler and put it above the get handler, but no luck, probably because they are different VERBs.
What are my options ?
Thanks.
Just lift out the middleware to its own function and use it in both routes.
function doLotsOfStuff (req, res) {
// do lots of stuff
}
app.get('/payment', doLotsOfStuff);
app.post('/payment', function(req, res, next) {
req.myvar = 'put something here';
next();
}, doLotsOfStuff);

Node.js matching the url pattern

I need an equivalent of following express.js code in simple node.js that I can use in middleware. I need to place some checks depending on the url and want to do it in a custom middleware.
app.get "/api/users/:username", (req,res) ->
req.params.username
I have the following code so far,
app.use (req,res,next)->
if url.parse(req.url,true).pathname is '/api/users/:username' #this wont be true as in the link there will be a actual username not ":username"
#my custom check that I want to apply
A trick would be to use this:
app.all '/api/users/:username', (req, res, next) ->
// your custom code here
next();
// followed by any other routes with the same patterns
app.get '/api/users/:username', (req,res) ->
...
If you only want to match GET requests, use app.get instead of app.all.
Or, if you only want to use the middleware on certain specific routes, you can use this (in JS this time):
var mySpecialMiddleware = function(req, res, next) {
// your check
next();
};
app.get('/api/users/:username', mySpecialMiddleware, function(req, res) {
...
});
EDIT another solution:
var mySpecialRoute = new express.Route('', '/api/users/:username');
app.use(function(req, res, next) {
if (mySpecialRoute.match(req.path)) {
// request matches your special route pattern
}
next();
});
But I don't see how this beats using app.all() as 'middleware'.
You can use node-js url-pattern module.
Make pattern:
var pattern = new UrlPattern('/stack/post(/:postId)');
Match pattern against url path:
pattern.match('/stack/post/22'); //{postId:'22'}
pattern.match('/stack/post/abc'); //{postId:'abc'}
pattern.match('/stack/post'); //{}
pattern.match('/stack/stack'); //null
For more information, see: https://www.npmjs.com/package/url-pattern
Just use the request and response objects as you would in a route handler for middleware, except call next() if you actually want the request to continue in the middleware stack.
app.use(function(req, res, next) {
if (req.path === '/path') {
// pass the request to routes
return next();
}
// you can redirect the request
res.redirect('/other/page');
// or change the route handler
req.url = '/new/path';
req.originalUrl // this stays the same even if URL is changed
});

Forward request to alternate request handler instead of redirect

I'm using Node.js with express and already know the existence of response.redirect().
However, I'm looking for more of a forward() functionality similar to java that takes the same parameters as redirect, but internally forwards the request instead of having the client perform the redirect.
To clarify, I am not doing a proxy to a different server. I'd like to forward('/other/path') directly within the same app instance
It wasn't apparently obvious how to do this from the express documentation. Any help?
You just need to invoke the corresponding route handler function.
Option 1: route multiple paths to the same handler function
function getDogs(req, res, next) {
//...
}}
app.get('/dogs', getDogs);
app.get('/canines', getDogs);
Option 2: Invoke a separate handler function manually/conditionally
app.get('/canines', function (req, res, next) {
if (something) {
//process one way
} else {
//do a manual "forward"
getDogs(req, res, next);
}
});
Option 3: call next('route')
If you carefully order your router patterns, you can call next('route'), which may achieve what you want. It basically says to express 'keep moving on down the router pattern list', instead of a call to next(), which says to express 'move down the middleware list (past the router)`.
You can implement forward (aka rewrite) functionality by changing request url property and calling next('route').
Note that the handler performing forward needs to be configured before other routes which you perform forwards to.
This is example of forwarding all *.html documents to routes without .html extension (suffix).
function forwards(req, res, next) {
if (/(?:.+?)\.html$/.test(req.url)) {
req.url = req.url.replace(/\.html$/, '');
}
next('route');
}
You call next('route') as the last operation. The next('route') passes control to subsequent routes.
As mentioned above, you need to configure forwards handler as one of the first handlers.
app.get('*', forwards);
// ...
app.get('/someroute', handler);
The above example will return the same content for /someroute as well as /someroute.html. You could also provide an object with a set of forward rules ({ '/path1': '/newpath1', '/path2': '/newpath2' }) and use them in forward mechanism.
Note that regular expression used in forwards function is simplified for mechanism presentation purposes. You would need to extend it (or perform check on req.path) if you would like to use querystring parameters etc.
I hope that will help.
For Express 4+
Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call
app.get("/a/path", function(req, res){
req.url = "/another/path";
app.handle(req, res);
}
Or for HTML5 mode of React/Angular
const dir = process.env.DIR || './build';
// Configure http server
let app = express();
app.use('/', express.static(dir));
// This route sends a 404 when looking for a missing file (ie a URL with a dot in it)
app.all('/*\.*', function (req, res) {
res.status(404).send('404 Not found');
});
// This route deals enables HTML5Mode by forwarding "missing" links to the index.html
app.all('/**', function (req, res) {
req.url = 'index.html';
app.handle(req, res);
});
Using the next function does not work if the next handler is not added in the right order. Instead of using next, I use the router to register the handlers and call
router.get("/a/path", function(req, res){
req.url = "/another/path";
router.handle(req, res);
}
Express 4+ with nested routers
Instead of having to use the outside of route/function app, you can use req.app.handle
"use strict";
const express = require("express");
const app = express();
//
// Nested Router 1
//
const routerOne = express.Router();
// /one/base
routerOne.get("/base", function (req, res, next) {
res.send("/one/base");
});
// This routes to same router (uses same req.baseUrl)
// /one/redirect-within-router -> /one/base
routerOne.get("/redirect-within-router", function (req, res, next) {
req.url = "/base";
next();
});
// This routes to same router (uses same req.baseUrl)
// /one/redirect-not-found -> /one/two/base (404: Not Found)
routerOne.get("/redirect-not-found", function (req, res, next) {
req.url = "/two/base";
next();
});
// Using the full URL
// /one/redirect-within-app -> /two/base
routerOne.get("/redirect-within-app", function (req, res, next) {
req.url = "/two/base";
// same as req.url = "/one/base";
//req.url = req.baseUrl + "/base";
req.app.handle(req, res);
});
// Using the full URL
// /one/redirect-app-base -> /base
routerOne.get("/redirect-app-base", function (req, res, next) {
req.url = "/base";
req.app.handle(req, res);
});
//
// Nested Router 2
//
const routerTwo = express.Router();
// /two/base
routerTwo.get("/base", function (req, res, next) {
res.send("/two/base");
});
// /base
app.get("/base", function (req, res, next) {
res.send("/base");
});
//
// Mount Routers
//
app.use("/one/", routerOne);
app.use("/two/", routerTwo);
// 404: Not found
app.all("*", function (req, res, next) {
res.status(404).send("404: Not Found");
});
app.get('/menzi', function (req, res, next) {
console.log('menzi2');
req.url = '/menzi/html/menzi.html';
// res.redirect('/menzi/html/menzi.html');
next();
});
This is my code:when user enter "/menzi",the server will give the page /menzi/html/menzi.html to user, but the url in the browser will not change;
You can use run-middleware module exactly for that. Just run the handler you want by using the URL & method & data.
https://www.npmjs.com/package/run-middleware
For example:
app.runMiddleware('/get-user/20',function(code,body,headers){
res.status(code).send(body)
})

Resources