Reputation: 11
I am beginner in nodejs and mongodb. I am using this tutorial scotch.io
to develop a restful api.
I got stuck at 'connect to our database'
mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o');
I have installed mongodb at - C:\mongodb
Data directory path - C:\Users\mshubham\Desktop\Main01\data\db
Project path (api) - C:\Users\mshubham\Desktop\Main01\testapi.js
Schema path - C:\Users\mshubham\Desktop\Main01\app\models
But I cannot get api request at localhost:8080/api/bears
Cannot GET /api/bears
I have tried all following combinations -
mongoose.connect('mongodb://localhost:27017/app');
mongoose.connect('mongodb://localhost:27017/app/models');
mongoose.connect('mongodb://localhost:27017/data');
[UPDATE] -
testapi.js - http://pastebin.com/6xgqWsfu
bear.js -
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BearSchema = new Schema({
name: String
});
module.exports = mongoose.model('Bear',BearSchema);
Upvotes: 1
Views: 300
Reputation: 48516
The error Cannot GET /api/bears
is not related mongodb connection, it means there is no get
route for /api/bears
, also I did not find this route in your codes posted in the link. Please add get()
as bellow following the original link.
router.route('/bears')
// create a bear (accessed at POST http://localhost:8080/api/bears)
.post(function(req, res) {
...
})
// get all the bears (accessed at GET http://localhost:8080/api/bears)
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
Upvotes: 1