Express Framework app.post和app.get [英] Express Framework app.post and app.get

查看:125
本文介绍了Express Framework app.post和app.get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于明确的框架来说是相当新鲜的。我在快速API参考中找不到application.post()方法的文档。有人可以提供几个可以参与的功能的例子吗?我已经阅读了几个网站,具有以下示例,第一个参数是什么意思?


  1. 我知道第二个参数是回调函数,但是我们究竟放在第一个参数?

      app.post('/',function(req,res){


  2. 同样,我们希望用户以特定的格式([{id:134123,url:www .qwer.com},{id:131211,url:www.asdf.com}))然后我们要从我们服务器的某个地方提取ID并从这些ID中检索数据,我们将如何编写app.post方法,它允许我们操纵对象数组的输入,这样我们只需要使用这些对象的ID(key)来检索必需的信息,而不管对象中的其他键如何,给定这个任务的描述,我们必须使用app.get()方法?如果是这样,我们如何编写app.get()函数?


谢谢

解决方案

1。 app.get('/',functi on(req,res){

这是告诉express来听取 / 的请求,并运行该函数看到一个。



第一个参数是一个匹配的模式。有时,如'/''/ privacy'的文字URL片段,您还可以进行如下所示的替换。如果需要,您还可以匹配正则表达式此处



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



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



变量替换:

  //这将匹配:
// / 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()

如果您将处理函数定义为函数(req,res,next){} ,可以调用 next()来产生,将请求传递回中间件链,你可以这样做,例如一个catchall路由:

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

再次,订单很重要,如果你想要的话,你必须把上面的其他路由功能



我以前没有POST过json,但Peter Peter的解决方案对我来说看起来不错。


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?

  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?

Thanks a lot for your inputs.

解决方案

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

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.

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.

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.

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:

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.

这篇关于Express Framework app.post和app.get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆