开发者

NodeJS Express DynamicView Helper with an http request

开发者 https://www.devze.com 2023-03-19 01:54 出处:网络
I hava a node.js app running on express with a dynamicViewHelper that hits an http service with http.request.Unfortunately, when I try to do this, because the httprequest is asynchronous and the funct

I hava a node.js app running on express with a dynamicViewHelper that hits an http service with http.request. Unfortunately, when I try to do this, because the http request is asynchronous and the function calling it is not, the page just renders before the http request is finished and I end up with undefined on my page.

I am basically looking for a way to put an asynchronous action (the http request), inside of a dynamic view helper which gets rendered out on the page. I know I can't just turn it into a synchronous function, so I am looking for another way to get that dynamic data to the view.

Also, is there a way to cache the data that is returned from an http.request()? I don't want to put it on the session, but I开发者_开发知识库 want any further requests made to the exact same URL to possibly be cached... Not as big of a deal as the first part of my question, though.


I figured it out. I'll just load it in my route so that res.render() isn't called until the request is done.

0

精彩评论

暂无评论...
验证码 换一张
取 消