将数据传递给支持的节点

时间:2017-06-08 12:40:30

标签: node.js http express post request

我不知道这里发生了什么,并试图将其修复几个小时。

我有一个简单的帖子 enter image description here

所以我将数据发布到

  

http://localhost:8080/api/data

现在在我的根文件夹中我有节点文件index.js我正在尝试检索已发布的值

  // grab the packages we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

// POST http://localhost:8080/api/users
// parameters sent with
app.post('/api/data', function(req, res) {
    var user_id = req.body.id;
    var token = req.body.token;
    var geo = req.body.geo;

    res.send(user_id + ' ' + token + ' ' + geo);
});

然而,当我启动节点服务器并导航到

  

http://localhost:8080/

  

http://localhost:8080/api/data

我很简单:Cannot Get

我做错了什么?

1 个答案:

答案 0 :(得分:0)

// POST http://localhost:8080/api/users
// parameters sent with
app.post('/api/data', function(req, res) {
  // ...
});
  

然而,当我启动节点服务器并导航到http://localhost:8080/http://localhost:8080/api/data时,我简单地得到:无法获取

当您导航到某个URL时,您的浏览器使用GET方法,而不是POST - 但是您的请求处理程序使用POST方法,而不是GET - 这意味着您的API不支持该路由的GET方法而且不是你在这看到什么。

如果您想使用浏览器进行测试,则需要使用正确的方法创建HTML表单或使用Postman或cUrl等工具: