搜索
Node.js+express建立博客时不能进入post发表博客页面
PHP中文网
PHP中文网 2017-04-17 12:07:08
[Node.js讨论组]

post页面显示为:Moved Temporarily. Redirecting to http://localhost:3000/
终端显示为(我用的ubuntu14.04):

user is not defined
    at __line (eval at <anonymous> (/home/lu/node/blog/node_modules/ejs/lib/ejs.js:464:12), <anonymous>:14:12)
    at eval (eval at <anonymous> (/home/lu/node/blog/node_modules/ejs/lib/ejs.js:464:12), <anonymous>:39:9)
    at returnedFn (/home/lu/node/blog/node_modules/ejs/lib/ejs.js:493:17)
    at View.exports.renderFile [as engine] (/home/lu/node/blog/node_modules/ejs/lib/ejs.js:350:31)
    at View.render (/home/lu/node/blog/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/home/lu/node/blog/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/home/lu/node/blog/node_modules/express/lib/response.js:801:7)
    at /home/lu/node/blog/routes/index.js:108:7
    at callbacks (/home/lu/node/blog/node_modules/express/lib/router/index.js:164:37)
    at param (/home/lu/node/blog/node_modules/express/lib/router/index.js:138:11)
GET /post 500 31ms - 100b

http.js:691
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
    at ServerResponse.res.setHeader (/home/lu/node/blog/node_modules/express/node_modules/connect/lib/patch.js:63:22)
    at /home/lu/node/blog/node_modules/express/node_modules/connect/lib/middleware/errorHandler.js:63:17
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

post.js代码:

var mongodb = require('./db');

function Post(name, title, post) {
    this,name = name;
    this.title = title;
    this.post =post;
}

module.exports = Post;

//存储一篇文章及其相关信息
Post.prototype.save = function (callback) {
    var date = new Date();
    //存储各种时间格式,方便以后扩展
    var time = {
        date: date,
        year: date.getFullYear(),
        month: date.getFullYear() + "-" + (date.getMonth() + 1),
             day: date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
        minute: date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "" + date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
    }
    //要存入数据库的文档
    var post = {
        name: this.name,
        time: time,
        title: this.title,
        post: this.post
    };
    //打开数据库
    mongodb.open(function (err, db) {
        if (err) {
            return callback(err);
        }
        //读取posts集合
        db.collection( 'posts', function (err, collection) {
            if (err) {
                mongodb.close();
                return callback(err);
            }
            //将文档插入posts集合
            collection.insert(post, {
                safe: true
            }, function (err) {
                mongodb.close();
                if (err) {
                    return callback(err);//失败!返回err
                }
                callback(null);//返回err为null
            });
        });
    });
};

//读取文章及其相关信息
Post.get = function(name, callback) {
    //打开数据库
    mongodb.open(function (err, db) {
        if (err) {
            return callback(err);
        }
        //读取posts集合
        db.collection('posts', function (err, collection) {
            if (err) {
                mongodb.close();
                return callback(err);
            }
            var query = {} ;
            if (name) {
                query.name = name;
            }
            //根据query对象查询文章
            collection.find(query).sort({
                time: -1
            }).toArray(function (err, docs) {
                mongodb.close();
                if (err) {
                    return callback(err);//失败!返回err
                }
                callback(null, docs );//成功!以数组形式返回查询的结果
            });
        });
    });
};

求解惑啊!!谢过了

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
PHPz
  1. 没发现问题,你的err_msg给全了吗;

  2. post.js第四行this,name = name;改为this.name = name;,试试;

  3. 根据错误信息,似乎是返回请求后,有地方在对响应头进行设置。
    只有这么多了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号