我在node.js中写了一个html页面,我希望对数据的响应感到高兴,但它更快.. 如何创建此功能同步?
app.get('/showGuides', function(req, res) {
text = fs.readFileSync('\start.html','utf8');
text = text + '<table border="1"><tr><td>id</td><td>name</td><td>last name</td><td>address</td><td>phone</td></tr>';
pool.getConnection(function(err, connection) {
var sql = 'select * from guides;';
console.log(sql);
connection.query( sql, function(err, rows) {
if (rows.length > 0) {
rows.forEach(function(row) {
console.log('add');
text = text + '<tr><td>' + row.id + '</td>';
text = text + '<td>' + row.name + '</td>';
text = text + '<td>' + row.lastName + '</td>';
text = text + '<td>' + row.address + '</td>';
text = text + '<td>' + row.phone + '</td></tr>';
});
}
connection.end();
});
});
text = text + '</table>'
text = text + fs.readFileSync('\end.html','utf8');
res.end(text);
});
答案 0 :(得分:1)
试试这个;
app.get('/showGuides', function (req, res) {
fetchGuides(function (err, guides) {
if (!err && guides) {
var text = fs.readFileSync('\start.html', 'utf8');
text += '<table border="1"><tr><td>id</td><td>name</td><td>last name</td><td>address</td><td>phone</td></tr>';
text += guides;
text += '</table>';
text += fs.readFileSync('\end.html', 'utf8');
res.end(text);
} else {
res.end('Unable to fetch guides');
}
});
});
function fetchGuides(cb) {
pool.getConnection(function (err, connection) {
var sql = 'select * from guides;';
console.log(sql);
var text = '';
connection.query(sql, function (err, rows) {
if (rows.length) {
rows.forEach(function (row) {
console.log('add');
text += '<tr><td>' + row.id + '</td>';
text += '<td>' + row.name + '</td>';
text += '<td>' + row.lastName + '</td>';
text += '<td>' + row.address + '</td>';
text += '<td>' + row.phone + '</td></tr>';
});
}
cb(err, text);
connection.end();
});
});
}
答案 1 :(得分:0)
哼哼似乎你只想呈现一个动态的html内容。在这种情况下,您可能更喜欢使用模板引擎,它会更简单。由于您已经在使用Express,这应该会有所帮助:
res.render('mytable.html', { rows: rows });
,在mytable.html中,你就可以循环查询行,语法取决于你的模板引擎。无论如何,您必须在查询回调中回复客户端请求(即res.end)。
答案 2 :(得分:0)
我已更新您的代码,以便在我之前的帖子中使用它。希望你能够很好地遵循它。但是,动态内容(例如从db检索的结果)最好使用模板处理,例如jade
或ejs
。
我将重构您的代码,以演示如何使用jade
模板在express中执行此操作。我已经发布了一些内容来使演示工作,您可以根据需要跳过/修改它们。 :)
独立脚本来处理/showGuides
请求:
var fs = require('fs');
var util = require('util');
var path = require('path');
var http = require('http');
var express = require('express');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// configure other middlewares you need
app.use(app.router);
process.on('uncaughtException', console.log);
var guides = {
process: function process(req, res, next) {
util.log('Received request to fetch guides');
guides.fetch(function (err, guides) {
req.guides = guides || [];
next(err);
});
},
fetch: function fetch(cb) {
pool.getConnection(function (err, connection) {
var sql = 'select * from guides;';
connection.query(sql, function (err, rows) {
cb(err, rows);
connection.end();
});
});
},
render: function render(req, res, next) {
res.locals.guides = req.guides;
res.render('guides');
},
errors: function errors(err, req, res, next) {
console.log(err);
res.locals.errmsg = err.message;
res.render('error');
}
};
app.get('/showGuides', guides.process, guides.render, guides.errors);
var server = http.createServer(app).listen(3000);
// dummy pool -- you don't need this
var pool = {
connection: {
query: function (query, cb) {
// dummy response -- simulate a db call
setTimeout(function () {
var dummies = [{
id: 1,
name: 'test',
lastName: 'test',
address: 'test',
phone: 1234
}, {
id: 2,
name: 'test2',
lastName: 'test2',
address: 'test2',
phone: 12345
}];
cb(null, dummies);
}, 500);
},
end: function () {
// nothing to do
}
},
getConnection: function (cb) {
cb(null, pool.connection);
}
};
现在我们需要添加一些jade模板,即guides.jade
,start.jade
,end.jade
和error.jade
。在目录views
下添加这些文件。
视图/ guides.jade:
include start
table(border="1", cellspacing="0", cellpadding="5px", width="50%")
tr
th id
th name
th last name
th address
th phone
each row in guides
tr
td= row.id
td= row.name
td= row.lastName
td= row.address
td= row.phone
include end
视图/ start.jade
//- Contents from start.html
h3 Guides
视图/ end.jade
//- contents from end.html
p Guides data rendered.
视图/ error.jade
h3 Oops! An error occurred
p= errmsg
如果您是jade的新手,请从jade tutorial开始。如果您无法遵循上述代码中的任何内容,请与我们联系。
美好的一天!