Streaming Results and res.render nodejs

假装没事ソ 提交于 2019-12-13 05:55:43

问题


I am using express and mysql-native with nodejs. With the mysql-native I stream results from the db row by row like this (in coffee):

exports.getUser = (username,callback) ->
 query = mysql.query "select * from users where username = #{username}"
 query.on "row", (row)->
  callback row

(obviously I would use prepared statements this is an example)

This sort of method won't work with res.render because you cannot be calling the res.render multiple times every time you get a new row

database.getUser "username", (result)->
  res.render "people", "data" : result 

Is there some other method for sending streamed data or would I have to stream results to the view with sockets?


回答1:


you can use your preferred templating engine directly

var row = _.template('<tr><td> <%= id > </td> </tr>');
...
database.getUser "username", (result)->
  res.write( row(result) );



回答2:


You may want to use Express partials.




回答3:


I figured it out last night in a moment of inspiration since I am streaming the data I can listen for when the stream ends and run my callback. That way I can push the data to an array then use in on a callback like so:

 query = mysql.query "select * from users where id = 2"
 data = []
 query.on "row", (row)->
   data.push row
 query.on "end", ()->
   callback data


来源:https://stackoverflow.com/questions/10204377/streaming-results-and-res-render-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!