cnak2 cnak2 - 26 days ago 11
Node.js Question

Get not working for Express using Mongoos

I'm new to to writing APIs with Express and Mongoos, but something that I think should work, is causing me some grief.

I'm trying to make things as modular as I can, so I have my main app.js, a model.js and then my routes.js

Here is my app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');

mongoose.connect('mongodb://localhost/guestbook');

var app = express();

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors());

app.use('/api', require('./routes/api'));


// changes it to use the optimized version for production
app.use(express.static(path.join(__dirname, '/dist')));

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;


Here is what I have in my /api (routes file)

//DEPENDENCIES
var express = require('express');
var router = express.Router();


//GUESTBOOK END POINTS
var Guestbook = require('../models/guestbook')

router.route('/guestbook')

.post(function(req, res){

var guestbook = new Guestbook();

guestbook.firstname = req.body.firstname;
guestbook.lastname = req.body.lastname;
guestbook.email = req.body.email;
guestbook.postedon = req.body.postedon;
guestbook.comment = req.body.comment;
guestbook.rate = req.body.rate;

guestbook.save(function(err){
if(err)
res.send(err);

res.json({message:'Post created!'})

});

})

.get(function(req, res){
guestbook.find(function(err, guestbook){
if(err)
res.send(err);

res.json(guestbook);
});

});





//RETURN ROUTER AS MODULE
module.exports = router;


Finally, here is my model:

//DEPENDENCIES
var restful = require('node-restful');
var mongoose = restful.mongoose;

var guestbookSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
postedon: Date,
comment: String,
rate: Number
});



//RETURN MODEL


module.exports = mongoose.model('guestbook', guestbookSchema);


Again, the post works, but the get doesn't.

Here is the console.log I get back for the get:

Error: No default engine was specified and no extension was provided.
at new View (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/view.js:62:11)
at EventEmitter.render (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/application.js:569:12)
at ServerResponse.render (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/response.js:961:7)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/app.js:32:13
at Layer.handle_error (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:310:13)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:330:12)
at next (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:310:13)
at /Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:330:12)
at Immediate.next (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:271:10)
at Immediate.<anonymous> (/Users/christophernakea/Documents/Projects/TestAPIExpress/server/node_modules/express/lib/router/index.js:618:15)
at Immediate.immediate._onImmediate (timers.js:435:18)

Answer

the probleme was that your are trying to render a page without specifying the template engine, since you are creating just an API, you don't need to render any page, so you need to just return a json res.render('error', { message: err.message, error: {}

you can replace it by res.json('error': err.message)