2015-11-01 21 views
0

我試圖讓POST請求使用快遞+ Node.js的。我安裝這些插件 的package.json如何使POST請求在節點JS +表達

{ 
    "name": "expressjs", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "body-parser": "^1.14.1", 
    "express": "^4.13.3", 
    "mongodb": "^2.0.47" 
    } 
} 

我做POST請求一樣,

var express = require('express'); 
var mongodb = require('mongodb'); 

var app = express(); 


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

var MongoClient = require('mongodb').MongoClient; 
var db; 

// Initialize connection once 
MongoClient.connect("mongodb://localhost:27017/abc", function(err, database) { 
    if(err) throw err; 

    db = database; 

    // Start the application after the database connection is ready 
    app.listen(3000); 
    console.log("Listening on port 3000"); 
}); 

app.post('/api/users', function(req, res) { 
    console.log('---') 
    var user_id = req.body.id; 
    var token = req.body.token; 
    var geo = req.body.geo; 

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

當我打的瀏覽器http://localhost:3000/api/users像沒有發生..

這裏是屏幕截圖enter image description here

我也嘗試鉻休息客戶端發送post請求..我這樣做在屏幕截圖。但我沒有收到任何控制檯消息或服務器的響應,我做錯了..

我從控制檯 偵聽端口3000

enter image description here

+0

1.爲什麼用jquery標記? 2.如果你想在瀏覽器中顯示某些東西,你需要創建app.get('/'callback())。 – 2015-11-01 14:46:52

+2

在瀏覽器中訪問它會產生一個GET請求(你沒有什麼東西在聽GET請求)。使用剩餘的客戶端 - 我相信你需要設置正確的Content-Type標題,否則Express將解析爲URL編碼,而不是JSON – Ian

+0

我想查看有關帖子沒有得到請求 – user944513

回答

0

第一圖爲GET請求只得到此消息,由於BR欠款默認爲GET。既然你不允許GET你只是一個通用的錯誤。

使用ReST客戶端,您需要將標頭設置爲Content-Type: application/json。然後這應該工作。