node.js 如何在 express、param 和 query 中从 url 获取 id 似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21497639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:47:40  来源:igfitidea点击:

how to get id from url in express, param and query doesnt seem to work

node.jsexpress

提问by jyoti

I have a url, i'm trying to get id but none of it is working req.params nor req.query

我有一个 url,我正在尝试获取 id 但它都不起作用 req.params 或 req.query

app.get('/test/:uid', function testfn(req, res, next) {
  debug('uid', req.params.uid);  // gives :uid
  debug('uid', req.query.uid);  // gives undefined      
});

I'm doing an ajax call like this

我正在做这样的ajax调用

$(document).on('click', 'a.testlink', function(e) {
  $.ajax({
    type: "GET",
    url: '/test/:uid',
    success: function(var) {
      console.log('success');
    },
    error: function() {
      alert('Error occured');
    }
  });
  return false;
});

I'm using

我正在使用

app.use(express.json());
app.use(express.urlencoded());

instead of body parser

而不是身体解析器

回答by grebneke

Your code is working as expected: The ajaxcall specifies url: '/test/:uid'which is what puts :uidin req.params.uid.

您的代码工作正常:该ajax调用指定url: '/test/:uid'这是放什么:uidreq.params.uid

Try sending something else: url: '/test/123'and req.params.uidwill contain 123

尝试别的东西送:url: '/test/123'req.params.uid将包含123

回答by SnapShot

Here is an example that will work. I will give step by step instructions from the start:

这是一个可以工作的示例。我将从一开始就逐步说明:

express myproject
cd myproject
npm install

Open app.js and add in the following somewhere in the file - maybe right before the line app.get('/test/:uid',test);

打开 app.js 并在文件中的某处添加以下内容 - 可能就在该行之前 app.get('/test/:uid',test);

var test = function(req,res,next) {
  // do whatever logic is needed 
  res.end('Displaying information for uid ' + req.params.uid);
}
app.get('/test/:uid',test);

Now, open up a new terminal, make sure you are in the myproject directory and enter:

现在,打开一个新终端,确保您在 myproject 目录中并输入:

node app.js

Now you can visit http://localhost:3000/test/45on the local machine and you should see:

现在你可以http://localhost:3000/test/45在本地机器上访问,你应该看到:

Displaying information for uid 45

If you are not accessing from your local machine make sure to change the url above to match whatever server your node app is running on.

如果您不是从本地机器访问,请确保更改上面的 url 以匹配您的节点应用程序正在运行的任何服务器。

Also, this is just a simple example. You might be better off organizing everything by placing the routes in files similar to the routes directory example setup in a new install of an express app. You can find more detailed examples of this on the web like thisone and thisone. Also, one of the best explanations of organizing/reusing code in Node this I have seen is in the book NodeJS in Action.

此外,这只是一个简单的例子。通过将路由放置在类似于新安装的 express 应用程序中的路由目录示例设置的文件中,您可能会更好地组织所有内容。你可以在网上找到更详细的例子,比如这个这个。此外,我所见过的关于在 Node 中组织/重用代码的最佳解释之一是在 NodeJS in Action 一书中。