【问题标题】:Passing data to node backed将数据传递给节点支持
【发布时间】:2017-06-08 12:40:30
【问题描述】:

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

我有一个简单的帖子

所以我将数据发布到

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

我做错了什么?

【问题讨论】:

  • get !== post,你确定你使用了正确的方法?

标签: node.js http express post request


【解决方案1】:
// 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 之类的工具:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 2023-03-21
    • 2018-01-15
    • 2018-03-28
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多