Pass URL as query parameter in node js - node.js

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)
})

Related

Support for a URL where the question mark to denote the start of queryparams is url encoded

I've got a Node/express application where our users can be directed to the application from Google Ads. The problem here is that they encode all "special characters" of the URL (Google have said this themselves).
I have an endpoint that looks like this:
app.get("/", (req, res) => {
res.send("Hello World!");
});
And I make a request to GET myurl.com/?foo=bar, my response will be what you'd expect ("Hello World!"). I can also easily pull the queryparams out of the request object.
However, if I have a campaign set up in Google Ads with some query parameters being tacked onto the end of the URL (i.e. UTM params), the request generated might look like GET myurl.com/%3Ffoo%3Dbar. Express out of the box doesn't automatically decode those characters, so when that URL gets hit, the express application will just return a 404 because there isn't an endpoint handler for /%3Ffoo%3Dbar.
What are my options here? Do I really have to write middleware to deal with this? I could use something like:
app.use(function(req, res, next) {
req.url = req.url.replace(<insert some regex here>, function() {
<insert some logic here to do the url decoding>
});
next();
});
But it feels weird to have to do this.

how to add query parameters to router.get

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

How to write url for request with params in express?

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'];
});

Dealing with slash characters in request parameter using Express route

I'm currently working on a URL shortener app using Express.
I want the user to be able to enter a URL like this:
https://www.exampleurlshortener.com/new/https://www.google.com
The problem is whenever I try to specify the parameter using Express it will only extract the 'https:' section and everything after that is lost because the 2 backslashes are registering as a new route:
app.get('/new/:url', (req, res) => {
console.log(req.params.url) // outputs 'https:'
I thought about specifying each section as a new parameter but if inner is blank this ends up throwing a 404. I would need to check if inner is blank using this method otherwise the user would be able to type https:/something/www.google.com
app.get('/new/:prot/:inner/:address', (req, res) => {
// throws 404 on valid addresses
Is there a simple way to solve this that I'm missing? Is the full URL available to be checked somewhere in the request? Or can parameters ignore backslashes?
You can use an expression to for your URL placeholders:
app.get('/new/:url(*)', (req, res) => {
console.log(req.params.url) // will output 'https://www.google.com'

How to modify request to include new url parameter

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.

Resources