我如何等待function

我在node.js中写了一个html页面,我想在对数据的响应中保留,但它更快。我怎样才能创build此function同步?

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); }); 

尝试这个;

 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(); }); }); } 

哼,似乎你只是想呈现一个dynamic的HTML内容。 在这种情况下,你可能更喜欢使用模板引擎,这会更简单。 正如您已经使用Express,这应该有所帮助:

无论如何,你的方法,你必须回答查询callback内的客户端请求(即res.end)。

我已经更新了你的代码,使它在我以前的文章中工作。 希望你能够很好的遵守。 但是,从db中检索结果等dynamic内容最好使用模板(如jadeejs

我会重构你的代码来演示如何使用jade模板来expression。 我扼杀了一些东西,使演示工作,你可以跳过/修改它们,只要你喜欢。 🙂

独立脚本来处理/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.jadeend.jadeend.jadeerror.jade 。 在dir 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 

如果你是玉的新手 ,从玉教程开始。 让我知道,如果你无法按照上面的代码中的任何东西。

美好的一天!