我是看教程学习的,做一个简易评论区,但教程中只说了如何提交数据,但没说如何获取。现在一头雾水。
流程应该是:a.启动(app.js)-->b.查询(bbs_a.js)-->c.路由(routes/bbs.js)-->d.显示(views/bbs.ejs)
但是不知道如何具体操作,对这几个步骤之间的关系也不甚清楚,网上的教程都是含糊带过,有朋友能讲解一下吗?非常感谢!
这是我写的代码
更新bbs_a.js,旧的是我单独摘出来的read,实际上有save和read
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 session = require('express-session');
var routes = require('./routes/index');
var bbs = require('./routes/bbs');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__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')));
app.use('/', routes);
app.use('/bbs', bbs);
app.listen(8000);
// 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 mysql = require('mysql');
var pool = mysql.createPool({
host: '127.0.0.1',
port: '3306',
user: 'root',
password: '',
database: 'sinatest'
});
function Comment() {};
module.exports = Comment;
pool.on('connection', function(connection) {
connection.query('SET SESSION auto_increment_increment=1');
});
pool.getConnection(function(err, connection) {
Comment.prototype.readComment = function (callback){
pool.query('SELECT comment FROM bbs', function(err, result) {
console.log("invoked[readComment]");
callback(err, result);
});
}
});
var express = require('express'),
router = express.Router(),
Comment = require('../bbs_a.js'),
TITLE_REG = '评论区';
router.get('/', function(req, res) {
console.log("1");
var comment = new Comment();
comment.readComment(function(err, result) {
if (err) {
console.log("2");
res.status(404).end(err);
} else {
console.log("3");
res.render('bbs', {
items: result
});
}
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<hr>
<h1>评论区</h1>
<ul>
<% for(var i=0; i<item.length; i++) { %>
<li><%=item[i].comment %></li>
<% } %>
</ul>
</body>
</html>
ps:我是看这个教程 http://www.cnblogs.com/zhongweiv/category/539147.html 学习的。写得很棒,特别是示例比较详细,而不只是讲原理。但并不能解决我的问题……
巴扎黑2017-04-17 11:42:03
Easy to use:
bbs.js
var express = require('express'),
router = express.Router(),
Comment = require('../bbs_a.js'),
TITLE_REG = '评论区';
router.get('/', function(req, res) {
var user = new User();//这里主要看你的构造方法是需要什么参数的
user.read(function(err,result){
if(err) {
res.status(404).end(err);
}else{
res.render('bbs', {
items: result
});
}
});
});
bbs.ejs
<ul>
<% for(var i=0; i<items.length; i++) { %>
<li><%=items[i].comment %></li>
<% } %>
</ul>
I haven’t tested it yet, it’s roughly like this. If you have any questions, please leave a message
天蓬老师2017-04-17 11:42:03
1. In bbs_a.js
, you need to export an object User
module.exports = User;
2. In bbs.js
, the object bbs_a.js
of
var User = require('bbs_a');
3. Call the User.read method to obtain data
router.get('/', function(req, res) {
var comment = User.read ... //调用数据库方法,获取数据
res.render('bbs', {
comment: comment //赋值,传到前台
});
});
Comment, your database method is a bit strange. . .
User.prototype.read = function read(callback)...
怪我咯2017-04-17 11:42:03
After modifying it like this, why do I still get an error?
var mysql = require('mysql');
var pool = mysql.createPool({
host: '127.0.0.1',
port: '3306',
user: 'root',
password: '',
database: 'sinatest'
});
function Comment() {};
module.exports = Comment;
pool.on('connection', function(connection) {
connection.query('SET SESSION auto_increment_increment=1');
});
pool.getConnection(function(err, connection) {
Comment.readComment = function readComment(callback) {
pool.query('SELECT comment FROM bbs', function(err, result) {
console.log("invoked[readComment]");
callback(err, result);
});
}
});
var express = require('express'),
router = express.Router(),
Comment = require('../bbs_a.js'),
TITLE_REG = '评论区';
router.get('/', function(req, res) {
var comment = new Comment();
comment.readComment(function(err,result){
if(err) {
res.status(404).end(err);
}else{
res.render('bbs', {
items: result
});
}
});
});
Error after running:
G:iamherebbs2node_modulesexpresslibrouterindex.js:446
throw new TypeError('Router.use() requires middleware function but got a
^ TypeError: Router.use() requires middleware function but got a Object