I have created a simple comment system in Express using MongoDB. The user simply uses the form on the home screen to enter a title & a comment and it then appears in a list on the bottom of the page.
However, Before it was simply showing the comments title and body on the page, what I wanted to try and do was to link the title so that when you clicked on it, it showed the comments content.
My model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CommentSchema = new Schema({
title: String,
content: String,
created: Date
});
module.exports = mongoose.model('Comment', CommentSchema);
My view:
extends layout
block content
h1= title
div.addCommentForm
form( method="post", action="/create")
div
div
span.label Title :
input(type="text", class="nameTxt", name="title")
div
span.label Comment :
textarea(name="comment")
div#addCommentSubmit
input(type="submit", value="Save")
br
br
#comments
- each comment in comments
div.comment
a(href=comment.title) #{comment.title}
div.name= comment.title
div.content= comment.content
hr
My app.js:
require('./models/comments'); // require the model before the 'index.js' file is called
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var create = require('./routes/create');
var show = require('./routes/show');
var app = express();
// Database stuff
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/comments-app');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/create', create);
**app.use('/:comment.title', show)**; // add to render the comments content
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// 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;
My show route:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Comment = mongoose.model('Comment', Comment);
router.get('/:comment.title', function(req, res) {
res.send(comment.content)
});
module.exports = router;
When I click on the comments title in my app, I am presented with this rather long error.
Not Found
404
Error: Not Found
at Layer.app.use.res.render.message [as handle] (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/app.js:37:15)
at trim_prefix (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:240:15)
at /Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:208:9
at Function.proto.process_params (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:269:12)
at next (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:199:19)
at next (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:176:38)
at next (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:176:38)
at /Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:137:5
at /Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:250:10
at next (/Users/Keva161/Documents/Projects/Webapps/Node/Express4-Comments-Mongoose/node_modules/express/lib/router/index.js:160:14)
Any ideas on why I am getting this error and how I can possibly fix it so I am getting the intended behaviour?
You're using
app.use('/:comment.title', show)
and then
router.get('/:comment.title', function(req, res)
You'll want to change one of these routes to just /
since this is saying use the route /:comment.title/:comment.title
You will probably also run into an issue with having the .title
. This will work if you intend on having a route like /SomeTitle.title
otherwise you may want to change it to /:title
so /SomeTitle
will work. I'm only speculating on your intentions for this. It's possible this is the way you intended it work.