I am very new to Express, I want to parse the url parameter. My code as below:
router.get('/', function (req, res) {
var aCustomerIdValue = req.param.aa;
console.log('In / Function be called: %s',aCustomerIdValue);
res.redirect('/checkouts/new');
});
But the console show "undefine". Any clue for it?
My express is 4.0.
Thank you in advanced!
Parameter types
req.params
It should be req.params.aa instead of req.param.aa - see:
http://expressjs.com/en/api.html#req.params
but it would work only for ":aa" parameters in the path of routes like router.get('/:aa', ...) - it would save XXX from request to GET /XXX in req.params.aa
req.query
For query parameters (most likely for GET requests) use req.query - see:
http://expressjs.com/en/api.html#req.query
It will save XXX from request to GET /?aa=XXX in req.query.aa
req.body
For the parameters passed in the request body use req.body - see:
http://expressjs.com/en/api.html#req.body
It will save aa parameters passed in the body of the request (most likely for POST requests) in req.body.aa
req.param()
There is also req.param('name') for either one of those, searched in order of:
req.params
req.body
req.query
but it is deprecated - thanks to Ben Fortune for pointing it out in the comment - see: http://expressjs.com/en/api.html#req.param
Your example
Try:
router.get('/', function (req, res) {
var aCustomerIdValue = req.query.aa;
console.log('In / Function be called: %s', aCustomerIdValue);
res.redirect('/checkouts/new');
});
if the parameter is passed in the query string, or:
router.get('/', function (req, res) {
var aCustomerIdValue = req.body.aa;
console.log('In / Function be called: %s', aCustomerIdValue);
res.redirect('/checkouts/new');
});
if the parameter is passed in the request body.
Make sure to put body-parser code in your server file above all the routes
Like this
var app=repress();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
And if you are passing query parameters then get it on server side by
req.query
And if you are passing parameters then get it server side by
req.params
Related
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!
I have a route where I built two GET APIs. I would like one to redirect from /download to /zip all while passing a parameter. The problem is I am getting a 404 for some reason the routes url is not being included in the redirect()
Here are the APIs.
// respond with xml from from static folder
router.get('/zip/:id', function (req, res) {
fileName = req.params.id
});
router.get('/download', function (req, res, next) {
var id = req.query.id
res.redirect('/zip?id='+ id);
});
module.exports = router;
I get a 404 when testing the URL:
localhost:8000/rest/pluto/v1/plugin/download?id=networktool
I am thinking it might be how I have the middleware setup but not real sure. I'm still new to node/express.
You are redirecting to a route that isn't actually defined. With your /zip/:id route definition:
router.get('/zip/:id', function (req, res) {
var fileName = req.params.id
});
The way that is defined, you have to have id information in the URL itself, so while the following routes would work:
/zip/networktool
/zip/1234
these routes would not:
/zip
/zip?id=networktool
/zip?id=1234
because Express is looking for the id to be built into the route itself. So you can do one of two things. You can either change your redirect to:
router.get('/download', function (req, res, next) {
res.redirect('/zip/'+ req.query.id);
});
or, you can modify your /zip route to make the id parameter optional with ?:
router.get('/zip/:id?', function (req, res) {
var fileName = (req.params.id) ? req.params.id : req.query.id;
});
I would recommend the first option, as the latter optional parameter only makes your zip route more complicated and require extra handling of whether id is actually passed to your route.
The path /zip/:id is expecting a path parameter not a query parameter.
You should redirect like this
res.redirect('/zip/'+ id);
I'm trying to get the value of a cookie with a cookie-parser.
The code is as follows (app.js):
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/', function (req, res) {
console.log('Cookies: ', req.cookies);
});
The log shows the cookies and their value but the page shows the error ERR_EMPTY_RESPONSE
If I do not use this, the web loads perfectly.
I hope you can help me, thanks in advance.
Every middleware function must either invoke the next one in the chain to continue request processing, or finish the response by itself (using res.end() or res.send(...) or similar functions). In your case you're not passing control to next middleware, so response is ending with your function - but you're not properly ending the response either. That's why the error.
If you just want to print cookie value, you can invoke the next middleware in chain by using :
app.get('/', function (req, res, next) {
console.log('Cookies: ', req.cookies);
next(); //--> Added to call next middleware in chain
});
Do you have this at the bottom?
app.listen(8080)
I want to get the value of parameters for BOTH POST and GET requests in Express/Node.js. I know of methods that will explicitly get POST or GET data, but I would like something that works for both. Is this possible in one line of code?
express.all('/page', function(req, res) {
var thing = req.body.thing; // only works for POST requests, not GET!
});
Thanks
You're looking for req.param(name, [defaultValue]).
From Express API Reference
Lookup is performed in the following order:
req.params
req.body
req.query
POST is req.body
GET is req.query
express.all('/page', function(req, res) {
var thing = req.param('thing');
});
Alternatively you can use req.body and req.query directly.
I am using Nodejs .
Server.js
app.get('/dashboard/:id', routes.dashboard);
Routes / index.js
exports.dashboard = function(req, res){
}
I want to be able to pass the 'id' variable from app.js to the dashboard function . How do I go about doing this ?
Assuming ExpressJS, you shouldn't need to pass it.
For each parameter placeholder (like :id), req.params should have a matching property holding the value:
exports.dashboard = function (req, res) {
console.log(req.params.id);
};
Though, this assumes the requested URL matches the route by verb and pattern.
Just ensure that your GET call from the client is something like this: /dashboard/12345.
12345 is the id you want to pass to dashboard.
So, you can access it like this in server:
exports.dashboard = function(req, res){
var id = req.params.id;
console.log('ID in dashboard: %s', id);
}