2017-06-08 17 views
0

我不知道這裏發生了什麼,現在試圖修復它幾個小時。將數據傳遞給支持的節點

我有一個簡單的後 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

我在做什麼錯?

+0

get!==後,你確定你使用正確的方法? –

回答

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我簡單的得到:不能得到

當您導航到你的瀏覽器使用GET方法的一些網址,而不是POST - 但是,您的請求處理程序使用POST方法,而不是GET - 這意味着您的API不支持該方法的GET方法,您不會在這裏看到。

如果你想與您的瀏覽器來測試它,你要麼需要創建一個HTML表單與正確的方法,或者使用一個工具,如郵差或捲曲:

+0

啊是的,沒有得到,謝謝 – Beep