admin管理员组文章数量:1127888
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'
});
};
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'
});
};
Share
Improve this question
edited Apr 24, 2018 at 14:06
Sebastian Simon
19.5k8 gold badges60 silver badges84 bronze badges
asked Nov 20, 2013 at 7:09
user2834795user2834795
4,5032 gold badges17 silver badges10 bronze badges
1
|
4 Answers
Reset to default 1050Express 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
You can do something like req.param('tagId')
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+ '!');
});
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
本文标签: javascriptHow to get a URL parameter in ExpressStack Overflow
版权声明:本文标题:javascript - How to get a URL parameter in Express? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736706918a1948726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/p?tagid=1234
, tagid is called a query string, not a URL parameter. A URL parameter would be/p/:tagId
. – mikemaccana Commented Dec 12, 2014 at 10:40