How to get the router values containing special characters in express router - node.js

I am working on to get route value in Nodes js using express framework
the url goes like
http://localhost:3000/course/view/turbine/turcV39/%20V42/%20V44/%20V47
Need to get the value "V39/%20V42/%20V44/%20V47" from the above url and route
router.get('/view/turbine/:turc?', function(req, res) {
console.log('a');
});

You can use regex to get it, like this
app.get(/\/view\/turbine\/turc(.*)/, function(req, res) {
console.log(req.params[0])
});

Related

nodejs get req.param from route

I am trying to get the url parameter of an url request from the front in my nodejs backend.
fetch(`http://localhost:9000/sent/5768797675645657`)
Here is my app.js file :
app.use('/sent/:id', require('./routes/sent'));
And my /routes/sent.js file :
router.get('/', function(req, res) {
console.log(req.params)
})
How can I get the console.log(req.params) to work?
So, app.use accepts two parameters: the portion of the URL, and a callback function.
In order to your app works, you've to change the /routes/sent.js and make it exports the function, so it can be used when you're requiring it.
Just change it to
module.exports = function (req, res) {
console.log(req.params)
}
and you're ready to go!

express.js route with or without a parameter

I use node.js v8.11.1 and express 4.16.3.
Say I have the following route
app.get('/:id', function(req, res){
I want to do something like
if(req.params.id) then query1
else //no id param in the url
query2
So, I could go either to http://localhost:3000/ or to http://localhost:3000/504 and the routes will respond accordingly.
but when I go to http://localhost:3000/ I just get Cannot GET /
How do I fix my routes?
Thanks
Make your route parameter optional using ? operator.
Change your route with following:
app.get('/:id?', function(req, res){
Now it should work for both: http://localhost:3000/ or http://localhost:3000/504
I agree with #n32303, you can do:
app.get('/', function(req, res){
//Called when there is no id specified
}
app.get('/:id', function(req, res){
// Called when an Id is specified (req.params.id will be set )
}
To eliminate the need for an if statement

ExpressJS Render Path with Hash

How can I set a url such as localhost:8080/foo#specialStuffHere from my ExpressJS application? I am using code in my router such as:
app.get('/foo/', function (req, res) {
res.render('foo', {myData: data});
});
Try using the command res.redirect(your url here) instead of res.render().

How do I use app.get to route to a new page on Nodejs and express

I've been trying to use app.get to route to a new html page like so:
e.g. when I type localhost:3000/newpage i want it to route to newpage.html
I've only been able to route to another JS file through app.get. Is it possible to do this but instead route to a html file? If not, is there a more appropriate method to doing this? I'm new to nodejs so any help at all would help!
What I have currently
app.js
var graball = require('./public/javascripts/graball')
app.get('/graball', function (req, res) {
res.send(ibm);
});
What I want
app.js
var page = require('./public/page1'); //page1.html
app.get('/page1', function(req, res) {
res.send(page1);
}
You can use sendFile:
app.get('/sitemap',function(req,res){
res.sendFile('/sitemap.html');
});

Express 4: router syntax

I am using Express 4 with the new router. At least one thing continues to confuse me, and it is a syntax problem - I am wondering if there is a regex that can do what I want. I have a standard REST api, but I want to add batch updates, so that I can send all the info to update some users models with one request, instead of one PUT request per user, for example. Anyway, I currently route all requests to the users resources, like so:
app.use('/users, userRoutes);
in userRoutes.js:
router.get('/', function (req, res, next) {
//gets all users
});
router.put('/:user_id', function (req, res, next) {
//updates a single user
});
but now I want a route that captures a batch request, something like this:
router.put('/Batch', function (req, res, next) {
//this picks up an array of users from the JSON in req.body and updates all
});
in other words, I want something which translates to:
app.use('/usersBatch, function(req,res,next){
}
...but with the new router. I can't get the syntax right.
I tried this:
app.use('/users*, userRoutes);
but that doesn't work. Does anyone know how to design this?
I'm guessing that the call to [PUT] /users/Batch is being picked up by the [PUT] /users/:user_id route. The string /:user_id is used as a regular expression causing it to also collect /Batch.
You can either move /Batch before /:user_id in the route order, refine the regex of /:user_id to not catch /Batch or change /Batch to something that won't get picked up too early.
(plus all the stuff Michael said)
REST doesn't include a POST as a list syntax. That's because each URL in REST point to an individual resource.
As an internet engineer I haven't seen any bulk PUTs or POSTs, but that said, it's your app, so you can make whatever API you like. There are definitely use cases for it.
You'll still need to describe it to Express. I would do it like this:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {}); // gets all users
router.post('/:user_id', function (req, res) {}); // one user
router.put('/:user_id', function (req, res) {}); // one user
router.patch('/:user_id', function (req, res) {}); // one user
router.delete('/:user_id', function (req, res) {}); // one user
app.use('/user', router); // Notice the /user/:user_id is *singular*
var pluralRouter = express.Router();
pluralRouter.post('/', function (req, res) {
// req.body is an array. Process this array with a foreach
// using more or less the same code you used in router.post()
});
pluralRouter.put('/', function (req, res) {
// req.body is another array. Have items in the array include
// their unique ID's. Process this array with a foreach
// using +/- the same code in router.put()
});
app.use('/users', pluralRouter); // Notice the PUT /users/ is *plural*
There are other ways to do this. Including putting comma-delimited parameters in the URL. E.g.
GET /user/1,2,3,4
But this isn't that awesome to use, and vague in a PUT or POST. Parallel arrays are evil.
All in all, it's a niche use case, so do what works best. Remember, the server is built to serve the client. Figure out what the client needs and serve.

Resources