Rumah > Soal Jawab > teks badan
语法错误❌吗? 头大
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Blog</title>
<link rel="stylesheet" href="../public/stylesheets/style.css">
</head>
<body>
<header>
<h1><%= title %></h1>
</header>
<nav>
<span><a title="主页" href="/">home</a></span>
<% if(user){ %>
<span><a title="发表" href="/post">post</a></span>
<span><a title="登出" href="/logout">logout</a></span>
<% } else { %>
<span><a title="登录" href="/login">login</a></span>
<span><a title="注册" href="/reg">register</a></span>
<% } %>
</nav>
<article>
<% if(success){ %>
<p><%= success %></p>
<% } %>
<% if(error){ %>
<p><%= error %></p>
<% } %>
app.js
/**
* 写法是在 app.js 中实现了简单的路由分配,然后再去 index.js 中找到对应的路由函数
* 最终实现路由功能。我们不妨把路由控制器和实现路由功能的函数都放到 index.js 里,app.js 中只有一个总的路由接口。
*/
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var settings = require('./settings');
var flash = require('connect-flash');
var users = require('./routes/users');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var app = express();
app.set('port',process.env.PORT || '3000');
// 视图引擎设置
app.set('views', path.join(__dirname, 'views'));
/*设置 views 文件夹为存放视图文件的目录, 即存放模板文件的地方,__dirname 为全局变量,存储当前正在执行的脚本所在的目录*/
app.set('view engine', 'ejs');//设置视图模板引擎为 ejs
app.use(flash());//("connect-flash": "0.1.1" )
//设置/public/favicon.ico为favicon图标。
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/*使用 express-session 和 connect-mongo 模块实现了将会化信息存储到mongoldb中。secret
用来防止篡改 cookie,key 的值为 cookie 的名字,通过设置 cookie 的 maxAge 值设定 cookie
的生存期,这里我们设置 cookie 的生存期为 30 天,设置它的 store 参数为 MongoStore
实例,把会话信息存储到数据库中,以避免丢失。在后面的小节中,我们可以通过 req.session
获取当前用户的会话对象,获取用户的相关信息。*/
app.use(session({
secret: settings.cookieSecret,
key: settings.db,//cookie name
cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
store: new MongoStore({
url: 'mongodb://localhost/blog'
})
}));
routes(app);
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
// 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
//开发环境下的错误处理器,将错误信息渲染error模版并显示到浏览器中
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: {}
});
});
//导出app实例供其他模块调用
module.exports = app;
index.js
/*/ :首页
/login :用户登录
/reg :用户注册
/post :发表文章
/logout :登出
*/
var crypto = require('crypto'),
User = require('../models/user.js')
,Post = require('../models/post.js');
module.exports = function(app) {
app.get('/', function (req, res) {
res.render('index', {
title: '主页',
user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
});
});
//reg :用户注册
app.get('/reg', checkNotLogin);
app.get('/reg', function (req, res) {
res.render('reg', {
title: '注册',
user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
});
});
app.post('/reg', checkNotLogin);
app.post('/reg', function (req, res) {
var name = req.body.name,
password = req.body.password,
password_re = req.body['password-repeat'];
if (password_re != password) {
req.flash('error', '两次输入的密码不一致!');
return res.redirect('/reg');
}
var md5 = crypto.createHash('md5'),
password = md5.update(req.body.password).digest('hex');
var newUser = new User({
name: name,
password: password,
email: req.body.email
});
User.get(newUser.name, function (err, user) {
if (err) {
req.flash('error', err);
return res.redirect('/');
}
if (user) {
req.flash('error', '用户已存在!');
return res.redirect('/reg');
}
newUser.save(function (err, user) {
if (err) {
req.flash('error', err);
return res.redirect('/reg');
}
req.session.user = user;
req.flash('success', '注册成功!');
res.redirect('/');
});
});
});
app.get('/login', checkNotLogin);
app.get('/login', function (req, res) {
res.render('login', {
title: '登录',
user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
});
});
app.post('/login', checkNotLogin);
app.post('/login', function (req, res) {
var md5 = crypto.createHash('md5'),
password = md5.update(req.body.password).digest('hex');
User.get(req.body.name, function (err, user) {
if (!user) {
req.flash('error', '用户不存在!');
return res.redirect('/login');
}
if (user.password != password) {
req.flash('error', '密码错误!');
return res.redirect('/login');
}
req.session.user = user;
req.flash('success', '登陆成功!');
res.redirect('/');
});
});
app.get('/post', checkLogin);
app.get('/post', function (req, res) {
res.render('post', {
title: '发表',
user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
});
});
app.post('/post', checkLogin);
app.post('/post', function (req, res) {
var currentUser = req.session.user,
post = new Post(currentUser.name, req.body.title, req.body.post);
post.save(function (err) {
if (err) {
req.flash('error', err);
return res.redirect('/');
}
req.flash('success', '发布成功!');
res.redirect('/');//发表成功跳转到主页
});
});
app.get('/logout', checkLogin);
app.get('/logout', function (req, res) {
req.session.user = null;
req.flash('success', '登出成功!');
res.redirect('/');
});
/*checkNotLogin 和 checkLogin 用来检测是否登陆,并通过 next() 转移控制权,检测到未登录
则跳转到登录页,检测到已登录则跳转到前一个页面。*/
function checkLogin(req, res, next) {
if (!req.session.user) {
req.flash('error', '未登录!');
res.redirect('/login');
}
next();
}
function checkNotLogin(req, res, next) {
if (req.session.user) {
req.flash('error', '已登录!');
res.redirect('back');
}
next();
}
};
/*注意:为了维护用户状态和 flash 的通知功能,我们给每个 ejs 模版文件传入了以下三个值:
*user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
* */
访问3000 会报
数据库MongoDB启动了!每次还是报
Tue, 25 Oct 2016 08:57:46 GMT express-session deprecated undefined resave option; provide resave option at app.js:40:9
Tue, 25 Oct 2016 08:57:46 GMT express-session deprecated undefined saveUninitialized option; provide saveUninitialized option at app.js:40:9
开源博客项目地址 https://github.com/nswbmw/N-b...
登录页面可以访问
注册页面不能注册®️
黄舟2017-04-10 17:42:46
你可以看我的 github 项目,说白了就是看这个教程做的,至少不会报错,报错直接找我解决,给你地址
https://github.com/OrangeXC/blog