Environment: MEAN tech stack
Hi, I want to add a query parameter to my router.get but I'm not sure how to define it.
Works like this right now:
http://test.com/path1/path2/1
router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/${req.params.userId}`;
I just want to add a search query parameter, would it be something like this?
http://test.com/path1/path2/1?q=test
And how would that get defined in the router.get?
You do not need to add a query parameter to your route directly. Just keep /path1/path2/:userId.
Within your function you can then check, if a query parameter exists, here via req.query.q.
// http://test.com/path1/path2/1?q=test
router.get('/path1/path2/:userId', (req, res) => {
let route = `GET /path1/path2/${req.params.userId}`;
// If http://test.com/path1/path2/1, req.query.q is undefined
console.log(req.params.userId, req.query.q);
});
You use the req.query object to get query parameters.
So, for the URL http://test.com/path1/path2/1?q=test, you could get the query parameter like this:
router.get('/path1/path2/:userId', (req, res) => {
console.log(req.params.userId); // "1"
console.log(req.query.q); // "test"
});
Doc for req.query is here.
For this url http://test.com/path1/path2/1?q=test
access path params = req.params.userId.
access query params = req.query.q.
Read more from Express documentation
http://expressjs.com/de/api.html#req.query
http://expressjs.com/de/api.html#req.params
Related
Express is great with req.query and creating an object of the query parameters. In this case, I just want those query parameters as a string. This is how I did it:
router.get('/*', (req, res) => {
const { originalUrl } = req;
console.log(originalUrl.slice(originalUrl.indexOf('?')));
But, I was wondering if there was a more direct or declarative way to do this. Surely this is not too uncommon of a task?
new URL("s://" + req.url).search gives you the query parameters, including the leading ?.
I am trying to hit the URL http://localhost:3000/analyze/imageurl=https://www.google.com/ from my browser.
However, due to the presence of //, it does not correctly hit the URL, and gives me an error message, Cannot GET /analyze/imageurl=https://www.google.com/
If I get rid of the backquotes as follows, http://localhost:3000/analyze/imageurl=httpswww.google.com/, it does work correctly.
My backend API looks like this
app.get('/analyze/:imageurl', function (req, res) {
console.log('printing image url:' + req.params.imageurl);
}
Is there a way I can pass in the imageurl with backquotes as a query parameter?
You need to encode your URL before pass it on query string, using encodeURIComponent. For example:
var urlParam = encodeURIComponent('https://www.google.com/');
console.log(urlParam); // https%3A%2F%2Fwww.google.com%2F
var url = 'http://localhost:3000/analyze/' + urlParam;
console.log(url); // http://localhost:3000/analyze/https%3A%2F%2Fwww.google.com%2F
// Decode it in API handler
console.log(decodeURIComponent(urlParam)); // https://www.google.com/
encodeURIComponent
One approach could be to use Express' req.query in your route. It would look something like this:
// Client makes a request to server
fetch('http://localhost:3000/analyze?imageurl=https://google.com')
// You are able to receive the value of specified parameter
// from req.query.<your_parameter>
app.get('/analyze', (req, res, next) => {
res.json(req.query.imageurl)
})
I want to make a get request for this type of url:
http://localhost:9000/data?start-time=1234124
I want to define route in ExpressJS:
app.get("/data",function(req,res){
})
How to set parameter start-time in url?
Thanks :)
Query string parameters are contained in the req.query object. If you want to get start-time, try this:
app.get("/data", function(req, res) {
let startTime = req.query['start-time'];
});
I want to add a new url parameter to the req.params before sending the response back . I tried the below way . But it's not working
router.get('/customers', function(req, res) {
req.params.customerId = someval ;
// proceed my implementation
});
Is there any specific way to do this or is this impossible to do ?
If you want to add a parameter in the request send to the server, you just have to :
router.get('/customers/:customerId', function(req, res) {
console.log(req.params.customerId);
});
If you want to add a parameter in the request to send back, so I don't know why you want to do that, but you can perhaps do this with setting the new parameters in req and call a res.redirect on the good route, or play with the res.location function.
I have an express route that looks like this:
app.get('/api/v1/username/:option', function(req, res) {
// do stuff
})
How can I modify this route so that the URL show the parameter name of option (option=)? For example:
http://localhost:8080/api/v1/johndoe/option=my-cool-option
That's a URL segment, not a parameter.
If you want it like you've shown the URL, it'd be
http://localhost:8080/api/v1/johndoe/?option=my-cool-option
Note the question mark ?, this specifies that it's a GET parameter.
app.get('/api/v1/:username', function(req, res) {
//req.params.username would equal 'johndoe'
//req.query.option would equal 'my-cool-option'
})