如何使用expressJS在“post”路由器中获得价值?

时间:2013-09-06 07:54:56

标签: javascript node.js express

我在nodejs中很新鲜,所以快递。

假设我从POST方法中获取返回值,我怎样才能从GET中获取它?我想, app.use(),我知道中间件只能处理请求,但当时我还不知道。

 ...
 var Some = require('./Some');
 app.get('/',function(req,res){
    res.render({
       title:"hi",
       output: data || ''    <--------I wanna get data from below
    })
 });

 app.post('/',function(req,res){         
     var some = new Some();
     some.postOriginCode(code,function(data){
         data               <-------- here is the data i want.

         //I can do it the way,but I don't like.
         res.render('index', {output:data});
     });
 });
 ...

2 个答案:

答案 0 :(得分:2)

var Some = require('./Some');
app.get('/',function(req,res){
   res.render({
   title:"hi",
   output:app.get('data')
})
});

app.post('/',function(req,res){         
 var some = new Some();
 some.postOriginCode(code,function(data){
      app.set('data',data);
   });
});

答案 1 :(得分:1)

如果您想处理GET&amp;同一路线中的​​POST适用于app.all

就像

一样
app.all('/',function(req,res){ 

       //processing code here 

});