2016-02-19 100 views
0

我需要一個API從Android插入數據到mongodb,我遵循this,代碼可以運行,沒有錯誤,並且我使用POSTMAN嘗試發佈一些數據,它總是說無法獲取任何答覆,似乎有錯誤連接到192.168.1.105:3000/songs ,但我可以使用查找所有api和findByID。 這花了我數小時的時間,請大家幫忙,所以壓低:(Mongodb插入數據API沒有響應

app.js

var express = require('express'); 
var bodyParser = require('body-parser'); 
var app = express(); 

songs = require('./routes/route'); 
var jsonParser = bodyParser.json(); 

app.post('/songs', jsonParser, function (req, res) { 
if (!req.body) return res.sendStatus(400); 


app.get('/', function (req, res) { 
res.send('Hello World!'); 
}); 

app.listen(3000, function() { 
console.log('Example app listening on port 3000!'); 
}); 


app.get('/songs',songs.findAll); 
app.get('/findById/:id',songs.findById); 
app.get('/songs',songs.addSong); 

路線

var mongoose = require('mongoose'); 
var mongo = require('mongodb'); 
var uri = "mongodb://XXXX:[email protected]:61365/aweitest"; 
mongoose.connect(uri); 
// we're connected! 
var db = mongoose.connection.db; 
var BSON = require('bson').BSONPure; 
var body = require('body-parser'); 


db.on('error', console.error.bind(console, 'connection errrrrrrrror:')); 
db.once('open', function() { 
console.log("mongodb is connected!!"); 
}); 

exports.findAll = function(req, res) { 
db.collection('songs',function(err, collection) { 
collection.find().toArray(function(err, items) { 
    res.send(items); 
    }); 
    }); 
    }; 


exports.findById = function(req, res) { 
var id = req.params.id; 
console.log('Retrieving song: ' + id); 
db.collection('songs', function(err, collection) 
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) { 
    res.send(item); 
}); 
}); 
}; 


exports.addSong = function(req, res) { 
var song = req.query; 
console.log('Adding song: ' + JSON.stringify(song)); 
db.collection('songs', function(err, collection) { 
collection.insert(song, {safe:true}, function(err, result) { 
{console.log('Success: ' + JSON.stringify(result[0])); 
res.send(result[0]); 

} 
}); 
}); 
}; 
+0

你能修正你的代碼中的縮進嗎?這真的很難閱讀。 – JohnnyHK

+0

對不起,1分鐘;) –

+0

@JohnnyHK好吧,我盡力而爲;) –

回答

1

好了,所以我在你的代碼改變了一些東西。我成功獲取app.js文件中的請求主體請看代碼

route.js

var mongoose = require('mongoose'); 
var mongo = require('mongodb'); 
var uri = "mongodb://localhost:27017/test"; 
mongoose.connect(uri); 
// we're connected! 
var db = mongoose.connection.db; 
var BSON = require('bson').BSONPure; 
var body = require('body-parser'); 
db.on('error', console.error.bind(console, 'connection errrrrrrrror:')); 
//db = mongoose.connection.db; 
db.once('open', function() { 
    console.log("mongodb is connected!!"); 
}); 

exports.addSong = function(req, res) { 
var song = req.body; 
console.log('Adding song: ' + JSON.stringify(song)); 
db.collection('songs', function(err, collection) { 
    collection.insert(song, {safe:true}, function(err, result) { 
     if (err) { 
     res.send({'error':'An error has occurred'}); 
     } else { 
     console.log('Success: ' + JSON.stringify(result[0])); 
     res.send(result[0]); 
     } 
    }); 
}); 
} 

請注意,我已將URI更改爲localhost,因此請將其更改爲您指定的URI。

app.js

var express = require('express'); 
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 
songs = require('./routes/route'); 

app.post('/songs', function(req, res) { 
    console.log("Request.Body : " + JSON.stringify(req.body)); 
    if (!req.body) return res.sendStatus(400); 
    songs.addSong(req, res); 
}); 

app.get('/', function (req, res) { 
    res.send('Hello World!'); 
}); 

app.listen(3000, function() { 
    console.log('Example app listening on port 3000!'); 
}); 

app.get('/songs',songs.addSong); 

由於您唯一的問題是,你是不是能夠得到請求主體。您將可以通過此代碼獲取它。

此外,由於你的app.get('/songs',songs.addSong);類型的GET,你就無法通過它來發送POST 數據。因此,將其更改爲

app.post('/songs',songs.addSong);

希望這有助於。

+0

它工作!哦,我的上帝,你讓我的一天! –