如何在快捷节点js中获取url参数

如何在快捷节点js中获取url参数

本文介绍了如何在快捷节点js中获取url参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对从我的网址获取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});

//所有环境
app.configure(function(){

app.set('port',process.env.PORT || 8888);
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
应用程序.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') );
});

控制器/ index.js: -

 函数控制器(params)
{
var app = params.app;
// var query_string = request.query.query_string;

app.get('/ p?tagId = /',function(request,response){
// userId是url请求中的一个参数
response.writeHead 200); // return 200 HTTP OK status
response.end('你正在寻找tagId'+ request.route.query.tagId);
});

}

module.exports = controller;

routes / index.js

  require('./ controllers'); 
/ *
* GET主页。
* /

exports.index = function(req,res){
res.render('index',{title:'Express'});
};


解决方案

Express 4.x



要获取URL参数的值,请使用

  app.get('/ p /:tagId',function(req,res){
res .send(tagId设置为+ req.params.tagId);
});

// GET / p / 5
// tagId设置为5

如果要获取查询参数?tagId = 5 ,请使用

  app.get('/ p',function(req,res ){
res.send(tagId设置为+ req.query.tagId);
});

// GET / p?tagId = 5
// tagId设置为5






Express 3.x



网址参数

  app.get('/ p /:tagId',function(req,res){
res.send(tagId is设置为+ req.param(tagId));
});

// GET / p / 5
// tagId设置为5

查询参数

  app.get('/ p',function(req,res){
res.send(tagId设置为+ req.query(tagId));
});

// GET / p?tagId = 5
// tagId设置为5


I am facing issue on getting 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' });
};
解决方案

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

这篇关于如何在快捷节点js中获取url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:30