ChatGPT解决这个技术问题 Extra ChatGPT

How to get a URL parameter in Express?

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'
  });
};
In express, /p?tagid=1234, tagid is called a query string, not a URL parameter. A URL parameter would be /p/:tagId.

m
maček

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

Which version of express are you using? I just tested this on express-3.4.4 and it works fine.
Remember to use /p/5 if you're using the top solution, or /p?tagId=5 if you're using the bottom solution.
The top solution(/p/5) works perfect for me..but bottom one /p?tagId=5 gives me error "tagId is set to undefined"
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".
req.param() is deprecated: Use either req.params, req.body or req.query, as applicable. For named route "parameters" (e.g. /p/:tagId) use req.params. For query strings (e.g. /p?tagId=5) use req.query.
t
tomahim

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


This method has been deprecated. Use req.query instead.
@alextc req.query and req.params is so different things, the replacement for req.param('x') is req.params.x not req.query.
M
Malatesh Patil

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+ '!');
});

L
LogicalBranch

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

generally people do not make url like this /p/:tagid=1234