如何在Express中获取URL参数?

时间:2013-11-20 07:09:12

标签: javascript node.js express url-parameters query-parameters

我遇到了从我的网址tagid获取localhost:8888/p?tagid=1234的价值的问题。

帮我修正我的控制器代码。我无法获得tagid值。

我的代码如下:

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

4 个答案:

答案 0 :(得分:582)

Express 4.x

要获取网址参数的值,请使用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

如果您想获取查询参数?tagId=5,请使用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

网址参数

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

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

查询参数

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

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

答案 1 :(得分:18)

您可以执行req.param('tagId')

之类的操作

答案 2 :(得分:5)

如果您想获取网址中的查询参数值,请按照以下代码进行操作

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

如果您想使用Express param功能

获取网址参数

Express param函数用于获取特定参数。这被视为中间件,将在调用路由之前运行。

这可以用于验证或获取有关项目的重要信息。

这方面的一个例子是:

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

答案 3 :(得分:1)

如果您的路线如下,这将有效:localhost:8888 / p?tagid = 1234

$-$$

如果你的路线是这样的话,这将有效:localhost:8888 / p /:tagid = 1234

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