Jak uzyskać parametr URL w programie Express?

Mam problem z uzyskaniem wartości tagid z mojego adresu URL: localhost:8888/p?tagid=1234.

Pomóż mi poprawić kod kontrolera. Nie jestem w stanie uzyskać tagid wartość.

Mój kod jest następujący:

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'
  });
};
Author: Xufox, 2013-11-20

4 answers

Express 4.x

Aby uzyskać wartość parametru URL, użyj 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

Jeśli chcesz uzyskać parametr zapytania ?tagId=5, Użyj req.zapytanie

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

Parametr URL

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

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

Parametr zapytania

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

// GET /p?tagId=5
// tagId is set to 5
 482
Author: maček,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-16 20:11:41

Możesz zrobić coś takiego req.param('tagId')

 16
Author: tomahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-20 07:12:30

Jeśli chcesz pobrać wartość parametru zapytania z adresu URL, wykonaj poniższe elementy kodu

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

Jeśli chcesz pobrać parametr URL za pomocą funkcji Express param

Express funkcja param, aby pobrać określony parametr. Jest to uważane za pośrednią i będzie przebiegać przed nazwaniem trasy.

Może być używany do walidacji lub przechwytywania ważnych informacji o elemencie.

Przykładem może być:

// 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+ '!');
});
 4
Author: Malatesh Patil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-06 12:38:53

To zadziała, jeśli Twoja trasa będzie wyglądać następująco: localhost: 8888 / p?tagid=1234

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

To zadziała, jeśli Twoja trasa będzie wyglądać następująco: localhost: 8888 / P/: tagid=1234

var tagId = req.params.tagid;
console.log(tagId); //1234
console.log(req.params.tagid); //1234
 1
Author: ajay saini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-18 11:18:30