express can`t get query param from url [duplicate] - javascript

This question already has answers here:
How to get GET (query string) variables in Express.js on Node.js?
(26 answers)
Closed 4 years ago.
I use express framework and react on front app for manage http request on node app. A have method :
app.get('/api/matches', async (req, res) => {
console.log(req.originalUrl); // /api/matches/
console.log(req.query); // {}
...
when I use url like http://localhost:3000/matches?id=123 I expect to get id inside req.query object but instead I get empty {} object. Also I tried to check how express see url using originUrl object, its return without query ?id=123.

You need to use your URL like http://localhost:3000/api/matches/?id=123. Notice that api word. This is because your GET route has /api/matches and request will look for path /api/matches. Doing that change will work for you. Then with that change you will be able to get req.query as {id: 123}

Related

Feature toggling using Expressjs middleware - frontend and backend

My current project is using Node for both frontend and backend and ExpressJS as the middleware.
I have a requirement where I need a feature toggling implementation to introduce some new features in my application. I am using a url parameter, e.g. &featureToggle=true to determine if the code of execution would be the new one or the existing.
Now I have parts in frontend and backend both which need to be changed based on the feature toggle. In the backend I can get the query object separately and extract the url param, similarly also in the frontend module.
Is there a way in which I can use Express to intercept the query param, set a variable value to either true or false based on the feature toggle, and which could be used across both the frontend and backend modules?
with express you can use req.query which gathers the query string sent in the request. You could pass it like this:
localhost:9000/path?&featureToggle=true
the ? is important it tells express that you are creating a query.
if you then place it into a variable:
const query = req.query
you would get the following output:
{ featureToggle: 'true' }
so as you can see it is returning an object.
you can check it like so:
if(req.query.featureToggle === 'true'){
runSomeCode();
};
or in your case if you want to run some kind of middleware:
router.get('/', (req, res, next) => {
if(req.query.featureToggle === 'true'){
return next(toggle)
}
};

Getting error 404 for put request (using express and node js) [duplicate]

I am trying to create two routes in my express app. One route, without a parameter will give me a list of choices, the other one with a parameter will give me the choice related to the id.
router.get('/api/choice', choice_controller.get_choices);
router.get('/api/choice/:id', choice_controller.get_choice);
When I go to .../api/choice/?id=1 the api returns the list of choices, and therefore follows the route without the param (/api/choice). How do I make sure that the router does not omit the parameter?
Thanks in advance.
UPDATE
It seems that it does not fire the /api/choice/:id route. If I remove the one without the param, it gives a 404 so. Could someone explain to me why /api/choice/?id=1 is not getting picked up by /api/choice/:id?
Basically, your declared routes are documented in the Express documentation.
The second route is resolved by a URL like /api/choice/hello where 'hello' is mapped into the req object object as:
router.get('/api/choice/:id', function (req, res) {
console.log("choice id is " + req.params.id);
});
What you are actually trying is mapping query parameters.
A URL like /api/choice/?id=1 is resolved by the first router you provided.
Query parameters are easy to get mapped against the request as:
router.get('/api/choice', function (req, res) {
console.log('id: ' + req.query.id);
//get the whole query as!
const queryStuff = JSON.stringify(req.query);
console.log(queryStuff)
});

How to retrieve query parameters from GET request using javascript? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 12 months ago.
Below is my GET request. I am trying to retrieve the client_id and redirect_uri parameters.
https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback
And then utilize those values, in a embedded js script within the same html page.
Config.set({
clientId: //fetched query parameter for client_id
redirectUri: // fetched query parameter for redirect_uri
});
If this is on the client you can use URL and searchParams
// const url = new URL(location.href); // uncomment and delete next line
const url = new URL("https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback"); // for example
const obj = {
"clientId": url.searchParams.get("client_id"),
"redirectUri": url.searchParams.get("redirect_uri")
};
console.log(obj)
// Config.set(obj)
If on the server: for example node also has URL
And here is an answer for php: Get URL query string parameters

Fetch Query Parameters [duplicate]

This question already has an answer here:
How do I parse a URL for a specific Query Paramter in javascript?
(1 answer)
Closed 2 years ago.
I need to fetch authorization code from the URL . It is present as a query string parameters.
When I run the belowo URL
https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113
It redirects to
http://localhost:8080/?code=8wFgU1GJo3
I need to parse the localhost URL and fetch the code.
Please help on how to retrieve the code
Code :
const url = 'https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113'
const config = {
method: "GET"
};
const response = await fetch(url ,config);
console.log('Response Text...............'+response.text())
You could use plain js URL web api to create URL object and then get the code value.
const url = 'http://localhost:8080/?code=8wFgU1GJo3'
const code = new URL(url).searchParams.getAll('code')
console.log(code)

Express server route format

I have two different types of possible API class I can make.
The first one is:
http://api_url.com/api/v1/schools/countries/BR
and the second one is:
http://api_url.com/api/v1/schools/countries/BR?admin1=MA
My route in backend/routes/schools.js is:
router.get('/countries/:country', forward_get);
const forward_get = (req, res, next) => {
console.log(req);
const url = `${url}${req.originalUrl}`
getResponse(url, acToken, res);
}
How do I make it so that I am able to also make the second api call and get the appropriate parameters "admin1: MA". Ive gone through the whole req object and I don't seem to find them anywhere. So far I've been able to make the first api call without a problem.
This is the only route you need:
You access admin1 using req.query.admin1
and
You access country using req.params.country

Categories

Resources