尝试使用内容标题向内容主体添加锚链接

时间:2014-08-30 16:18:20

标签: javascript node.js mongodb express pug

我使用MongoDB在Express中创建了一个简单的评论系统。用户只需使用主屏幕上的表格输入标题&评论然后它出现在页面底部的列表中。

然而,在它只是在页面上显示评论标题和正文之前,我想要尝试做的是链接标题,这样当你点击它时,它就会显示评论内容。

我的模特:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CommentSchema = new Schema({
    title: String,
    content: String,
    created: Date
});

module.exports = mongoose.model('Comment', CommentSchema);

我的观点:

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

我的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;

我的节目路线:

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;

当我点击我的应用中的评论标题时,我会看到这个相当长的错误。

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)

关于我为什么会收到此错误以及如何解决此问题以便获得预期行为的任何想法?

1 个答案:

答案 0 :(得分:0)

您正在使用

app.use('/:comment.title', show)

然后

router.get('/:comment.title', function(req, res)

您想要将其中一条路线更改为/,因为这是说使用路线/:comment.title/:comment.title 拥有.title可能也会遇到问题。如果您打算使用类似/SomeTitle.title的路线,则可以使用此功能,否则您可能需要将其更改为/:title,以便/SomeTitle能够正常工作。我只是在猜测你对此的意图。这可能是你想要它的工作方式。