2016-04-26 47 views
0

我想在Azure移动应用程序Node.js后端服务器中使用easy api。 但努力后,我无法获得请求的内容。Azure移动应用程序node.js后端 - 无法获取请求的内容

任何人都可以告诉我我失去了什么可能的东西吗?

这是我的API(myApi.js):

module.exports = { 
 
    "post": function (req, res, next) { 
 
     for (var k in req) { 
 
      console.log("%s: %j", k, req.k); 
 
     } 
 
     res.status(200).send('post'); 
 
    } 
 
}

这是我的app.js:

var express = require('express'), 
 
    azureMobileApps = require('azure-mobile-apps'); 
 

 
var app = express(); 
 

 
var mobile = azureMobileApps({ 
 
    // Explicitly enable the Azure Mobile Apps home page 
 
    homePage: true 
 
}); 
 

 
mobile.tables.import('./tables'); 
 

 
mobile.api.import('./api'); 
 

 
mobile.tables.initialize() 
 
    .then(function() { 
 
     app.use(mobile); // Register the Azure Mobile Apps middleware 
 
     app.listen(process.env.PORT || 3000); // Listen for requests 
 
    });

我用邮差发送请求。 邮差
enter image description here

这是我的控制台日志,你可以看到,请求一切是不确定的。 PARAMS:未定义 查询:未定义 头:未定义 网址:未定义 的StatusCode:未定义 体:未定义 ...

+0

删除'then'后的'function'和 再试一次。就像这样:。然后((){ app.use(mobile); app.listen(process.env.PORT || 3000); }); –

回答

2

您需要在移动应用程序服务器中安装body-parser模块,在移动应用程序入口app.js中配置身体分析器中间件。然后,您可以使用req.body获取帖子正文内容。

你可以尝试以下步骤:

  1. 登录捻控制台网站或移动应用服务器的Visual Studio的在线编辑器修改脚本并运行npm命令。在您的根目录下添加body解析器模块,如"body-parser": "^1.13.1"dependencies部分中的package.json文件。运行npm update来安装依赖关系。
  2. 添加语句中app.js

    var bodyParser = require('body-parser'); 
    app.use(bodyParser.json({ limit: '50mb' })); 
    app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));` 
    
  3. 尝试的代码片段在你方便的API:

    module.exports = { 
        "post":function(req,res,next){ 
         console.log(req.body) 
         res.send(req.body) 
        } 
    }; 
    
  4. 在邮差,使用x-www-form-urlencoded来发表您的数据: enter image description here

+0

嗨GaryLiu 感谢您的回答,这真的解决了我的问题。 你确实节省了我的时间。 – Aaron

+0

这对我有用。我认为我的错误是我认为仅仅是因为我看到body-parser坐在节点模块中,它是正确的版本,它会起作用。我认为将它添加到package.json并运行npm install(用于本地安装)就可以实现。 –

0

尝试console.log("%s: %j", k, req[k]);代替。

原因是变量k是一个字符串。因此,要通过密钥作为字符串访问成员,您必须使用“数组符号”

+0

嗨JasonWihardja, 感谢您的回答。 我尝试了这个之后,我可以看到一些请求的价值。 但是请求[“params”],请求[“body”]等内容仍然没有任何内容。 如何获取邮递员发送的请求内容?谢谢。 – Aaron

相关问题