I am facing an issue on getting the value of tagid
from my URL: localhost:8888/p?tagid=1234
.
我正面临从我的URL: localhost:8888/p获取tagid值的问题。
Help me out to correct my controller code. I am not able to get the tagid
value.
帮我修正我的控制器代码。我无法获得tagid值。
My code is as follows:
我的代码如下:
app.js
:
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
:
控制器/ 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
:
路线/ index.js:
require('./controllers');
/*
* GET home page.
*/
exports.index = function(req, res) {
res.render('index', {
title: 'Express'
});
};
4 个解决方案
#1
434
Express 4.x
表达4.倍
To get a URL parameter's value, use req.params
要获取URL参数的值,请使用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
如果您想获取一个查询参数?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
表达3.倍
URL parameter
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
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
#2
14
You can do something like req.param('tagId')
可以执行req.param('tagId')之类的操作
#3
3
If you want to grab the query parameter value in the URL, follow below code pieces
如果您想在URL中获取查询参数值,请遵循下面的代码片段
//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函数获取URL参数。
Express param function to grab a specific parameter. This is considered middleware and will run before the route is called.
表示param函数以获取特定的参数。这被认为是中间件,将在调用路由之前运行。
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+ '!');
});
#4
1
this will work if your route is like: localhost:8888/p?tagid=1234
如果您的路由是:localhost:8888/p
var tagId = req.query.tagid;
console.log(tagId);// 1234
console.log(req.query.tagid);// 1234
OR
this will work if your route is like: localhost:8888/p/:tagid=1234
如果您的路线是:localhost:8888/p/:tagid=1234,那么这将起作用。
var tagId = req.params.tagid;
console.log(tagId); //1234
console.log(req.params.tagid); //1234
#1
434
Express 4.x
表达4.倍
To get a URL parameter's value, use req.params
要获取URL参数的值,请使用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
如果您想获取一个查询参数?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
表达3.倍
URL parameter
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
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
#2
14
You can do something like req.param('tagId')
可以执行req.param('tagId')之类的操作
#3
3
If you want to grab the query parameter value in the URL, follow below code pieces
如果您想在URL中获取查询参数值,请遵循下面的代码片段
//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函数获取URL参数。
Express param function to grab a specific parameter. This is considered middleware and will run before the route is called.
表示param函数以获取特定的参数。这被认为是中间件,将在调用路由之前运行。
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+ '!');
});
#4
1
this will work if your route is like: localhost:8888/p?tagid=1234
如果您的路由是:localhost:8888/p
var tagId = req.query.tagid;
console.log(tagId);// 1234
console.log(req.query.tagid);// 1234
OR
this will work if your route is like: localhost:8888/p/:tagid=1234
如果您的路线是:localhost:8888/p/:tagid=1234,那么这将起作用。
var tagId = req.params.tagid;
console.log(tagId); //1234
console.log(req.params.tagid); //1234