I have an express route that I want to find a uid value and for some reason it isn't working I was wondering what I was doing wrong. I keep getting an error whenever I got to localhost:8080/user?uid=20 and I want to retrieve 20 as the param uid but keep getting an error.
My path
/user?uid=:uid\d+
The code itself
router.get('/user\?uid=:uid\d+', (req,res) => {
res.send('This works');
});
Having a regex patter for catching query string is not a good practice. Reduce your route to /user, then you can have access to your query strings through req, just like this:
for localhost:3000/user?uuid=20
router.get('/user', (req,res) => {
console.log(req.query); // { uuid: 20 }
console.log(req.query.uuid); // 20
res.send('This works');
});
You can have more information here: https://expressjs.com/es/api.html#req.query
Related
I want to get a random character from MongoDB, but my call keeps getting used in another GET method.
// Get Specific Character by ID
router.get('/:char_id', async (req, res, next) => {
try {
const character = await Character.find({ char_id: req.params.char_id });
if (character.length == 0) return res.status(404).json({ message: 'No characters found with ID: ' + req.params.char_id });
else { res.status(200).json(character) };
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// Get Random Character
router.get('/random', async (req, res, next) => {
try {
const characters = await Character.find();
if (characters.length == 0) return res.status(404).json({ message: 'No characters found in the database. Something must have went wrong.' });
else {
const random = Math.floor(Math.random() * characters.length);
res.status(200).json(characters[random]);
};
} catch (err) {
res.status(500).json({ message: err.message });
}
});
When I call /characters/random, I get this error because first GET method above tries to pass word random to find character by ID (If I'm not wrong).
"message": "Cast to Number failed for value "random" (type string) at path "char_id" for model "Character""
How do I avoid this?
You can just put the second route above the first route. That works in similar cases for me.
A route like this:
router.get('/:char_id', ...)
is a wildcard route. It matches EVERYTHING at its level. So, it will match your intended ID, but it will also match /random. Thus, it "eats" your /random request and that request never gets to your actual handler for /random.
A quick fix here is to make sure the wildcard route is LAST on the router. That way, all the other routes get first dibs at the incoming request and only the left-over requests that didn't match some other handler will go to your wildcard route.
This is the desired order of your route declarations:
router.get('/random', async (req, res, next) => { ... });
// put this one last on the router
router.get('/:char_id', async (req, res, next) => { ... });
Even better would be to not use a wildcard route (that matches everything) at all, but rather define a regex for your char_id that would ONLY match your intended id strings and would not match other route paths.
I am trying to get route parameters working with express, i have the following code trying to use the colon to define the parameters, if I do just the station id it works app.get('/:stationId/ but adding on the radius part just doesn't return anything. What am i doing wrong?
app.get('/:stationId/asset?radius=:radius', (req, res) => {
console.log(req.params.stationId)
console.log(req.params.radius)
})
I know I would be able to change the url to be app.get('/:stationId/:radius but i need it in the other format.
Parameters after the ? are handled differently:
app.get("/:stationId/asset", (req, res) => {
console.log(req.params.stationId);
console.log(req.query.radius);
});
¿can you help me to understand this?
If I have an endpoint in a specific route it will display in the browser the expected content, but with the same code just modifying the endpoint it doesn't return anything.
The only change that I do is the route of the endpoint, the rest of code is the same in both cases.
¿Why this happen?
The code displays in the browser the content of a json file:
this way it doesn't work:
It doesn't show an error, just don't return anything.
localhost:3000/v1/people/partner
app.get("/v1/people/partner", (req, res) => {
const peopleInVisual = PeopleController.getAllVisualPeople();
res.json({ peopleInVisual });
});
In this way it works fine: notice I just have changed the hello at the endpoint
localhost:3000/v1/hello/partner
app.get("/v1/hello/partner", (req, res) => {
const PeopleInVisual = PeopleController.getAllVisualPeople();
res.json({ PeopleInVisual });
});
Thanks!
How do I handle a route that has a parameter, and if the client fails to include it, not let the server error out.
router.get('/:id', function(req, res, next) {
let profileID = req.params.id
if (profileID) {
res.send('ID provided');
} else {
res.send('No id provided');
}
});
If the ID parameter is provided I want to use it somewhere. But if it is missing, I want there to be a default behaviour to fall back to (ie. Route to another page, some message, etc)
The expected behavior is:
http://example.com/ routes to a landing page
But if a trailing ID is provided
http://example.com/abc123 routes to a specific profile
You can specify router.get('/:id?') and then id will be marked as an optional param.
CodeSandbox example
Express Server Example
// the question mark marks the parameter as optional
app.get("/:id?", (req, res) => {
// will create an id entry even if none is provided
console.log(req.params);
// if there is an id do something else
if (req.params.id) return res.send(req.params.id);
// if there is no id default behaviour for '/' route
res.sendFile(__dirname + "/view/hello.html");
});
the delete route:
router.delete('/users/:name' , function (req, res, next) {
User.deleteOne({name: req.params.name}).then (function (user) {
console.log('DELETED / ', req.params.name);
res.send('DELETED / ', req.params.name);
}).catch (next)
});
the router.get and router.post under the same '/users/' work no problem.
I get a strange error when I try this,
{
"error": "Unexpected token n in JSON at position 3"
}
although I have a 200 OK status response. Any idea what is going on? I'm trying in postman.
UPDATE:
Lesson learned here. Make sure your testing methods are in fact correct.
I was sending a different header that somehow got mixed up in postman which causes errors. It was a hard to notice at first but clicking the setting I discovered there as a strange extra huge JSON batch being sent back. Even though at first glance everything seemed ok
Try like this :
router.delete('/users/:name' , function (req, res) {
User.deleteOne({name: req.params.name})
.exec()
.catch (err => res.status(500).send(err) )
.then (function () {
console.log('DELETED / ', req.params.name);
res.send('DELETED / ', req.params.name);
})
});
Based on the Express API reference of res.send():
When the parameter is an Array or Object, Express responds with the JSON representation
Your code above seems to send the string "DELETED / " back. Maybe that is the reason why your JS code raise JSON parse error.
Try replace res.send statement with this one below:
res.send({msg : 'DELETED / ', user: req.params.name});
Hope this helps.