为什么不发这个帖子?

时间:2014-07-22 19:56:03

标签: mongodb express mongoose pug

我正在使用mongodb + Express,并在最新版本的Express中重写旧的评论应用程序。

由于最新Express的变化,我不得不改变一些事情。但我有一些问题。

基本上,当我提交表单时,它不会发布到/create。这可能是一个简单的修复,但任何帮助将不胜感激:)

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 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);

/// 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;

评论.js(模特)

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

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

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

index.jade

extends layout

block content
  h1= title
  div.addCommentForm
          form( method="post", action="/create")
          input(type='text', class='nameTxt', name='username')
  div
          span.label Comment :
          textarea(name='comment')
  div#addCommentSubmit
          input(type='submit', value='Save')
br
br
#comments
  - each comment in comments
    div.comment
      div.name comment.username
      div.created_at= comment.created
      br
      div.content= comment.content
      hr

create.js(路线)

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Comment = mongoose.model('Comment', Comment);


router.route('/create')

    .post(function(req, res) {
        var Comment = new Comment()
        username : req.body.username;
        content : req.body.comment;
        created : Date.now();

        Comment.save(function(err) { 
            if (err)
                res.send(err);
            res.send('Comment added');
        });
    });

module.exports = router;

1 个答案:

答案 0 :(得分:0)

form标记的玉缩进已关闭。确保HTML中input标记内的form标记比玉石中的form标记缩进,以便最终成为form标记的子标记在HTML中。

<强>糟糕

这块玉石

form
input

产生此HTML

<form></form><input>

<强>固定

form
  input

产量

<form><input></form>