Express Framework app.post和app.get

时间:2021-07-20 10:35:25

I am fairly new to the express framework. I couldn't find the documentation for application.post() method in the express API reference. Can someone provide a few examples of all the possible parameters I can put in the function? I've read a couple sites with the following example, what does the first parameter mean?

我对快递框架相当新。我在express API参考中找不到application.post()方法的文档。有人可以提供我可以在函数中放入的所有可能参数的一些示例吗?我用以下示例阅读了几个网站,第一个参数是什么意思?

  1. I know the second parameter is the callback function, but what exactly do we put in the first parameter?

    我知道第二个参数是回调函数,但我们究竟在第一个参数中添加了什么?

    app.post('/', function(req, res){
    
  2. Also, let's say we want the users to post(send data to our server) ID numbers with a certain format([{id:134123, url:www.qwer.com},{id:131211,url:www.asdf.com}]). We then want to extract the ID's and retrieves the data with those ID's from somewhere in our server. How would we write the app.post method that allows us to manipulate the input of an array of objects, so that we only use those object's ID(key) to retrieve the necessary info regardless of other keys in the objects. Given the description of the task, do we have to use app.get() method? If so, how would we write the app.get() function?

    此外,假设我们希望用户以特定格式发布(向我们的服务器发送数据)ID号([{id:134123,url:www.qwer.com},{id:131211,url:www.asdf。 COM}])。然后,我们想要提取ID并从我们服务器的某个位置使用这些ID检索数据。我们如何编写app.post方法,允许我们操作对象数组的输入,这样我们只使用这些对象的ID(键)来检索必要的信息,而不管对象中的其他键。鉴于任务的描述,我们是否必须使用app.get()方法?如果是这样,我们将如何编写app.get()函数?

Thanks a lot for your inputs.

非常感谢您的投入。

2 个解决方案

#1


7  

1. app.get('/', function(req, res){
This is telling express to listen for requests to / and run the function when it sees one.

1. app.get('/',function(req,res){这告诉express要监听对/的请求,并在看到它时运行该函数。

The first argument is a pattern to match. Sometimes a literal URL fragment like '/' or '/privacy', you can also do substitutions as shown below. You can also match regexes if necessary as described here.

第一个参数是匹配的模式。有时像“/”或“/ privacy”这样的文字网址片段,您也可以进行替换,如下所示。如有必要,您还可以匹配正则表达式,如此处所述。

All the internal parts of Express follow the function(req, res, next) pattern. An incoming request starts at the top of the middleware chain (e.g. bodyParser) and gets passed along until something sends a response, or express gets to the end of the chain and 404's.

Express的所有内部部分都遵循函数(req,res,next)模式。传入的请求从中间件链的顶部开始(例如bodyParser)并传递,直到某些东西发送响应,或表达到达链的末尾和404。

You usually put your app.router at the bottom of the chain. Once Express gets there it starts matching the request against all the app.get('path'..., app.post('path'... etc, in the order which they were set up.

您通常将app.router放在链的底部。一旦Express到达那里,它开始匹配所有app.get('path'...,app.post('path'...等)的请求,按照它们的设置顺序。

Variable substitution:

// this would match:
// /questions/18087696/express-framework-app-post-and-app-get

app.get('/questions/:id/:slug', function(req, res, next){
  db.fetch(req.params.id, function(err, question){
    console.log('Fetched question: '+req.params.slug');
    res.locals.question = question;
    res.render('question-view');
  });
});

next():
If you defined your handling functions as function(req, res, next){} you can call next() to yield, passing the request back into the middleware chain. You might do this for e.g. a catchall route:

next():如果你将处理函数定义为函数(req,res,next){},你可以调用next()来生成,将请求传递回中间件链。你可以这样做,例如一条美食路线:

app.all('*', function(req, res, next){
  if(req.secure !== true) {
    res.redirect('https://'+req.host+req.originalUrl);
  } else {
    next();
  };
});

Again, order matters, you'll have to put this above the other routing functions if you want it to run before those.

同样,订单很重要,如果您希望它在那些之前运行,您必须将其置于其他路由功能之上。

I haven't POSTed json before but @PeterLyon's solution looks fine to me for that.

我之前没有张过json,但@ PeterLyon的解决方案对我来说很好。

#2


6  

TJ annoyingly documents this as app.VERB(path, [callback...], callback in the express docs, so search the express docs for that. I'm not going to copy/paste them here. It's his unfriendly way of saying that app.get, app.post, app.put, etc all have the same function signature, and there are one of these methods for each supported method from HTTP.

TJ恼人地将此文件记录为app.VERB(路径,[回调...],快递文档中的回调,因此请搜索快递文档。我不打算将它们复制/粘贴到此处。这是他不友好的说法app.get,app.post,app.put等都具有相同的函数签名,并且HTTP中的每个受支持的方法都有这些方法之一。

To get your posted JSON data, use the bodyParser middleware:

要获取发布的JSON数据,请使用bodyParser中间件:

app.post('/yourPath', express.bodyParser(), function (req, res) {
  //req.body is your array of objects now:
  // [{id:134123, url:'www.qwer.com'},{id:131211,url:'www.asdf.com'}]
});

#1


7  

1. app.get('/', function(req, res){
This is telling express to listen for requests to / and run the function when it sees one.

1. app.get('/',function(req,res){这告诉express要监听对/的请求,并在看到它时运行该函数。

The first argument is a pattern to match. Sometimes a literal URL fragment like '/' or '/privacy', you can also do substitutions as shown below. You can also match regexes if necessary as described here.

第一个参数是匹配的模式。有时像“/”或“/ privacy”这样的文字网址片段,您也可以进行替换,如下所示。如有必要,您还可以匹配正则表达式,如此处所述。

All the internal parts of Express follow the function(req, res, next) pattern. An incoming request starts at the top of the middleware chain (e.g. bodyParser) and gets passed along until something sends a response, or express gets to the end of the chain and 404's.

Express的所有内部部分都遵循函数(req,res,next)模式。传入的请求从中间件链的顶部开始(例如bodyParser)并传递,直到某些东西发送响应,或表达到达链的末尾和404。

You usually put your app.router at the bottom of the chain. Once Express gets there it starts matching the request against all the app.get('path'..., app.post('path'... etc, in the order which they were set up.

您通常将app.router放在链的底部。一旦Express到达那里,它开始匹配所有app.get('path'...,app.post('path'...等)的请求,按照它们的设置顺序。

Variable substitution:

// this would match:
// /questions/18087696/express-framework-app-post-and-app-get

app.get('/questions/:id/:slug', function(req, res, next){
  db.fetch(req.params.id, function(err, question){
    console.log('Fetched question: '+req.params.slug');
    res.locals.question = question;
    res.render('question-view');
  });
});

next():
If you defined your handling functions as function(req, res, next){} you can call next() to yield, passing the request back into the middleware chain. You might do this for e.g. a catchall route:

next():如果你将处理函数定义为函数(req,res,next){},你可以调用next()来生成,将请求传递回中间件链。你可以这样做,例如一条美食路线:

app.all('*', function(req, res, next){
  if(req.secure !== true) {
    res.redirect('https://'+req.host+req.originalUrl);
  } else {
    next();
  };
});

Again, order matters, you'll have to put this above the other routing functions if you want it to run before those.

同样,订单很重要,如果您希望它在那些之前运行,您必须将其置于其他路由功能之上。

I haven't POSTed json before but @PeterLyon's solution looks fine to me for that.

我之前没有张过json,但@ PeterLyon的解决方案对我来说很好。

#2


6  

TJ annoyingly documents this as app.VERB(path, [callback...], callback in the express docs, so search the express docs for that. I'm not going to copy/paste them here. It's his unfriendly way of saying that app.get, app.post, app.put, etc all have the same function signature, and there are one of these methods for each supported method from HTTP.

TJ恼人地将此文件记录为app.VERB(路径,[回调...],快递文档中的回调,因此请搜索快递文档。我不打算将它们复制/粘贴到此处。这是他不友好的说法app.get,app.post,app.put等都具有相同的函数签名,并且HTTP中的每个受支持的方法都有这些方法之一。

To get your posted JSON data, use the bodyParser middleware:

要获取发布的JSON数据,请使用bodyParser中间件:

app.post('/yourPath', express.bodyParser(), function (req, res) {
  //req.body is your array of objects now:
  // [{id:134123, url:'www.qwer.com'},{id:131211,url:'www.asdf.com'}]
});