Express JS url parameter variable issue - javascript

I have this user route
const express = require('express');
const router = express.Router();
const {
GetAll,
} = require('../areas/directory/controllers/usercontroller');
router.route('/getall?:username&:email').get(GetAll);
module.exports = router;
When I try to access the url in Postman like this: http://localhost:5000/api/user/getall?username=nameone&email=emailone
I get this error: Cannot GET /api/user/getall
But, if I change it to
router.route('/getall/:username&:email').get(GetAll);
and access the url in Postman like this: http://localhost:5000/api/user/getall/username=nameone&email=emailone
it works.
On the other hand, even if it works, I am unable to get the value of my variable.
var username = req.params.username;
will return username=nameone instead.

For http://localhost:5000/api/user/getall?username=nameone&email=emailone to work, you should change
router.route('/getall?:username&:email').get(GetAll);
to
"router.route('/getall').get(GetAll);"
and use req.query.username to access the value of username query parameter and req.query.email to access the value of email query parameter.
After making these changes, you can call http://localhost:5000/api/user/getall?username=nameone&email=emailone in Postman, you will be able to see the value of username and email in your code.
This is because you need not have to specify the query parameters in the path, like ?:username in router.route('getall').
Edit: adding few more details about path and query
Please see the top 2 solutions for this question to learn more about path and query and why you should change your code to the way I mentioned above : here is the link.

Reason for the error...
Actually the thing what you are trying to do is called query passing in the rest api..
But you are trying to access them like params
sollution 💖
follow the steps to access the queries
**To pass the data using ? in the api request should be like this you can have multiple query objects
http://localhost:5000/getall?email='email'&&password='password'
Access the query in express app like this
app.post('/getall', (req, res) => {
const {
email,
password
} = req.query
return res.status(200).json({
email,
password
})
})
The req.query contains the data you trying to pass throw ? and then query properties
Response of above code would be like this
{
"email": "'email'",
"password": "'password'"
}

You should not give query parameters in URL. So, for express the url must be just /getall and in your code you access those variables using req.params.whatever_query_parameter

Related

Object of which I have a working method for is somehow undefined? Javascript app.patch request [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)
});

Steam API Request Failed [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 do I exclude additional properties using express-validator?

I am using express-validator in my express app, and I am attempting to prevent additional fields being specified on POST requests. This is because I am passing the value of req.body to my ORM to insert into the database, and I want to avoid having to explicitly map between my inserted object and request body alongside adding validators.
Is this possible? I can't seem to find it in the documentation. Using JSON Schema you can do this with additionalProperties: false
After some more research, I found that it's possible to do with with the Matched Data API
In my express controller I can now do;
const { matchedData } = require('express-validator');
(req, res) => {
const matched = matchedData(req, {
includeOptionals: true,
});
db.insert(matched)
...
}

Firebase Functions Express : How to get URL params with paths

I have a url in the form www.a.com/users/uid, I want to get uid.
Assume a function :
exports.smartlink = functions.https.onRequest((req, res) =>
Doing req.params returns an empty array, whereas req.query.uid when the url contains query strings works.
If your URL is "www.a.com/users/uid", "uid" will be part of req.path. You can split that on '/' to get the uid as the last element of the array returned by split.
You can only use req.params with Cloud Functions for Firebase when you're exporting an entire Express app that defines a router that uses a placeholder for the element in the path you want to extract.
Alternatively, you can use express in firebase. and use req.param to get the value.

How to read parameters from URL in a specific way with express JS

I created a server with the express package, and I'm trying to read in a specific way the parameters from the URL.
The URL goes like this: http://127.0.0.1:8080/screen=3 (no '?' as the sign for parameters).
I need to save the number of the screen in a variable.
I tried this:
var express = require('express');
var app = express();
app.get('/screen:sceenNum', function (req, res) {
var temp = req.sceenNum;
res.send(temp); //for checking on the browser
});
I also tried this, but he must get '?' in the URL:
app.get('/', function(req, res) {
var screenNum = req.param('screen');
res.send(screenNum);
});
Can anyone please have a solution?
Thank you
You can access route/url parameters with the req.params object.
So a route like /screen:screenNum would accept urls like /screen3 and you would access 3 via req.params.screenNum.
Similarly, if you want to use the equals, just add that: /screen=:screenNum and the number is accessed the same.

Categories