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 );//成功!以数组形式返回查询的结果
});
});
});
};
function Post(name, title, post) {
this,name = name;
this.title = title;
this.post =post;
}
this,name
什么鬼~
看你程序发生的错误是user is not defined
,然后看下面的调用堆栈at __line (eval at <anonymous> (/home/lu/node/blog/node_modules/ejs/lib/ejs.js:464:12), <anonymous>:14:12)
可能是渲染ejs
模板引擎的时候出的问题。
你看看模板引擎里面定义了些什么,然后在渲染的时候传递过去是否正确。