429

I am facing an issue on getting the value of tagid from my URL: localhost:8888/p?tagid=1234.

Help me out to correct my controller code. I am not able to get the tagid value.

My code is as follows:

app.js:

var express = require('express'),
  http = require('http'),
  path = require('path');
var app = express();
var controller = require('./controller')({
  app: app
});

// all environments
app.configure(function() {
  app.set('port', process.env.PORT || 8888);
  app.use(express.json());
  app.use(express.urlencoded());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
  app.set('view engine', 'jade');
  app.set('views', __dirname + '/views');
  app.use(app.router);
  app.get('/', function(req, res) {
    res.render('index');
  });
});
http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});

Controller/index.js:

function controller(params) {
  var app = params.app;
  //var query_string = request.query.query_string;

  app.get('/p?tagId=/', function(request, response) {
    // userId is a parameter in the url request
    response.writeHead(200); // return 200 HTTP OK status
    response.end('You are looking for tagId' + request.route.query.tagId);
  });
}

module.exports = controller;

routes/index.js:

require('./controllers');
/*
 * GET home page.
 */

exports.index = function(req, res) {
  res.render('index', {
    title: 'Express'
  });
};
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user2834795
  • 4,333
  • 2
  • 15
  • 10
  • 23
    In express, `/p?tagid=1234`, tagid is called a **query string**, not a URL parameter. A URL parameter would be `/p/:tagId`. – mikemaccana Dec 12 '14 at 10:40

4 Answers4

1014

Express 4.x

To get a URL parameter's value, use req.params

app.get('/p/:tagId', function(req, res) {
  res.send("tagId is set to " + req.params.tagId);
});

// GET /p/5
// tagId is set to 5

If you want to get a query parameter ?tagId=5, then use req.query

app.get('/p', function(req, res) {
  res.send("tagId is set to " + req.query.tagId);
});

// GET /p?tagId=5
// tagId is set to 5

Express 3.x

URL parameter

app.get('/p/:tagId', function(req, res) {
  res.send("tagId is set to " + req.param("tagId"));
});

// GET /p/5
// tagId is set to 5

Query parameter

app.get('/p', function(req, res) {
  res.send("tagId is set to " + req.query("tagId"));
});

// GET /p?tagId=5
// tagId is set to 5
maček
  • 76,434
  • 37
  • 167
  • 198
  • Which version of express are you using? I just tested this on `express-3.4.4` and it works fine. – maček Nov 20 '13 at 07:20
  • Remember to use `/p/5` if you're using the top solution, or `/p?tagId=5` if you're using the bottom solution. – maček Nov 20 '13 at 07:23
  • The top solution(/p/5) works perfect for me..but bottom one /p?tagId=5 gives me error "tagId is set to undefined" – user2834795 Nov 20 '13 at 07:24
  • @user2834795, I'm not sure why you're having difficulty with `req.param`, I've added a `req.query` solution for you as well. – maček Nov 20 '13 at 07:27
  • 2
    Thanks alot macek.you save my time.It was my mistake, i did all according to you but in url i was using "tagid" rather than "tagId". – user2834795 Nov 20 '13 at 07:38
  • 12
    `req.param()` is [deprecated](http://expressjs.com/api.html#req.param): `Use either req.params, req.body or req.query, as applicable.` For named route "parameters" (e.g. `/p/:tagId`) use [`req.params`](http://expressjs.com/api.html#req.params). For query strings (e.g. `/p?tagId=5`) use [`req.query`](http://expressjs.com/api.html#req.query). – Nateowami Jun 19 '15 at 08:18
  • What about /myurl/:guid/blahblha/sdfsd – King Friday Jan 03 '18 at 16:28
  • I think that would be `app.get('/myurl/:guid/blahblha/sdfsd', function(req, res) { req.params.guid... })` – haven't used express in some time – maček Jan 03 '18 at 21:12
  • req.query("someparam") gives me an error "req.query is not a function". What works for me is req.query.someparam. I'm using express 4.16.3. I realize this is almost a 5-year old question and just as old an answer. – Ya. May 02 '18 at 18:31
23

You can do something like req.param('tagId')

tomahim
  • 1,298
  • 2
  • 11
  • 29
22

If you want to grab the query parameter value in the URL, follow below code pieces

//url.localhost:8888/p?tagid=1234
req.query.tagid
OR
req.param.tagid

If you want to grab the URL parameter using Express param function

Express param function to grab a specific parameter. This is considered middleware and will run before the route is called.

This can be used for validations or grabbing important information about item.

An example for this would be:

// parameter middleware that will run before the next routes
app.param('tagid', function(req, res, next, tagid) {

// check if the tagid exists
// do some validations
// add something to the tagid
var modified = tagid+ '123';

// save name to the request
req.tagid= modified;

next();
});

// http://localhost:8080/api/tags/98
app.get('/api/tags/:tagid', function(req, res) {
// the tagid was found and is available in req.tagid
res.send('New tag id ' + req.tagid+ '!');
});
Malatesh Patil
  • 4,505
  • 1
  • 14
  • 14
15

This will work if your route looks like this: localhost:8888/p?tagid=1234

var tagId = req.query.tagid;
console.log(tagId); // outputs: 1234
console.log(req.query.tagid); // outputs: 1234

Otherwise use the following code if your route looks like this: localhost:8888/p/1234

var tagId = req.params.tagid;
console.log(tagId); // outputs: 1234
console.log(req.params.tagid); // outputs: 1234
Malekai
  • 4,765
  • 5
  • 25
  • 60
ajay saini
  • 277
  • 1
  • 3
  • 7