Get params using ExpressJS - node.js

I'd like to get a specific param using ExpressJS with "#" instead of "?" in the url...
My URL :
http://localhost:3000/#access_token=LMkdfkdmsklmfdkslklmdskfmsda
I'd like to get "access_token" and "req.params.access_token" doesn't work...
Anthony

Short answer: you can't.
Longer answer: fragment identifiers (that's the part after the #) are supposed to be evaluated on the client and are not supposed to be sent to server. Your express app has no way of knowing them.
You could try to convert them to query parameters or path variables (i.e. by handling fragment identifier change in javascript) to make them visible server-side.

Related

Why URL module ignores characters after # in node.js

I am using url module, which basically splits a web address into readable part.
var data = url.parse(request.url).pathname
the output of request.url is C:\AppFolder\dropbox\videos\myVideo8#.MP4. After its get parsed, I dont understand why its not returing the value with "#.MP4"
I dont understand why its not returing the value with "#.MP4"
Because #.MP4 is the fragment and not the path component of the URL. (You can read up on URL syntax f.e. on WikiPedia, if you are not sure: https://en.wikipedia.org/wiki/URL#Syntax)
You want to look at hash, not pathname https://nodejs.org/docs/latest/api/url.html#url_url_hash

Get wrong parameter from get method in nodejs

I'm trying to send get method request and want to pass value in URL.
Like my api look like
app.get('/api/getlocation/:customerName', customer.getlocation);
For call this I wrote in postman
localhost:8080/api/getlocation/:customerName=kumbhani
For test
var customerName = req.params.customerName;
console.log('name', customerName); // =kumbhani
It returns name with = sign - I want only kumbhani
The colon character in the path in Express has a special meaning: whatever you put in the URL after getLocation/ will be put in req.params.customerName.
This means in Postman, you should actually call this URL:
localhost:8080/api/getlocation/kumbhani
→ See related question.

get values from client side to server side (node.js + express.js)

I'm still trying to understand the concepts of node.js so please don't blame me if this is a dumb question..
In node.js, is it possible to get a value from index.jade to index.js?
For example:
index.jade
a(href="/bla" name="someName") Blabla
index.js
router.get('/bla', function(req, res){
//get value of name ("someName") or string ("Blabla")
console.log(req.body.name) ??
});
If this is not possible, I would like to know why...
Thanks.
No, it's not possible, for the simple reason that the name attribute in your HTML doesn't get passed to the server (any server, not necessarily a Node-based server).
If you want to pass a value in a GET request, you generally pass it as part of the URL:
a(href="/bla?name=someName") Blabla
This will generate the following HTML:
Blabla
In your server code, you can access the value using req.query.name.
Taking this a step further: if you have a variable available to your template called "name", you can use something similar, but a bit more dynamic:
a(href="/bla?name=" + encodeURIComponent(name)) Blabla
encodeURIComponent makes sure that any "special" characters (that may have a special meaning in URL's) will be encoded properly.

GET a website url when routing

Sorry, my vocabulary is very limited, any help clarifying this question is deeply appreciated.
I'm building a server using Nodejs and Express, it has a route like /new/:url. I access the value passed on the url by using req.params.url. This works well for simple strings, like chocolate, however, if I pass a website url, like http://www.google.com, then it won't be routed to /new/:url.
Question: how can I pass a website url and access it with Node/Express?
Edit: I am trying to use the GET method, and apparently a way to solve this problem is through Wildcards/Regex.
Thank you very much for helping!
Use Post method.
Set header "Content-Type" : "application/json"
Set body { "urlblahblah~" : "https://www.google.com" }
Then parse It as JSON in server-side
you can use Javascripts encodeURIComponent, so when you passing to your server on client, you will allsways encode the url, so you can pass it as regular parameter. or as mentioned by Hulk if posting is an options you can pass it as body param as well...
var url = encodeURIComponent("http://some.url/asdasa?asdas=12312")
will result in :
"http%3A%2F%2Fsome.url%2Fasdasa%3Fasdas%3D12312"
which is safe for passing as param
The solution that worked for me was Regex. Instead of routing as /new/:url, I used:
/new/:url(*)
So that if http://www.google.com is given as a parameter:
req.params.url = "http://www.google.com"

Node.js : Express app.get with multiple query parameters

I want to query the yelp api, and have the following route:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
When I make a GET request to
http://localhost:3000/yelp?term=food&location=austin,
I get the error
Cannot GET /yelp?term=food&location=austin
What am I doing wrong?
Have you tried calling it like this?
http://localhost:30000/yelp/term/food/location/austin
The URL you need to call usually looks pretty much like the route, you could also change it to:
/yelp/:location/:term
To make it a little prettier:
http://localhost:30000/yelp/austin/food
In the requested url http://localhost:3000/yelp?term=food&location=austin
base url/address is localhost:3000
route used for matching is /yelp
querystring url-encoded data is ?term=food&location=austin i.e. data is everything after ?
Query strings are not considered when peforming these matches, for example "GET /" would match the following route, as would "GET /?name=tobi".
So you should either :
use app.get("/yelp") and extract the term and location from req.query like req.query.term
use app.get("/yelp/term/:term/location/:location") but modify the url accordingly as luto described.
I want to add to #luto's answer. There is no need to define query string parameters in the route. For instance the route /a will handle the request for /a?q=value.
The url parameters is a shortcut to define all the matches for a pattern of route so the route /a/:b will match
/a/b
/a/c
/a/anything
it wont match
/a/b/something or /a
Express 4.18.1 update:
using app.get("/yelp/term/:term/location/:location"), your query string can be yelp/term/food/location/austin
So your request url will look like this:
http://localhost:3000/yelp/term/food/location/austin

Resources