synchronous https request

2,489 views
Skip to first unread message

axs

unread,
May 30, 2011, 11:45:06 PM5/30/11
to nod...@googlegroups.com
Hi all, 

I'm writing a little app in node.js that needs to make an https request at the beginning to get a parameter from a web page. This parameter is used all throughout the code, so I want to avoid using the async https.request with callbacks. I'm afraid this isn't possible, but I hope there is some way to do it! What is the lightest way of doing this synchronously? I don't mind blocking for several seconds as this request is done only once, upon initialization. 

Thanks in advance for any help.

Regards,
axs

mscdex

unread,
May 31, 2011, 12:20:58 AM5/31/11
to nodejs
On May 30, 11:45 pm, axs <alexst...@gmail.com> wrote:
> I'm writing a little app in node.js that needs to make an https request at
> the beginning to get a parameter from a web page. This parameter is used all
> throughout the code, so I want to avoid using the async https.request with
> callbacks. I'm afraid this isn't possible, but I hope there is some way to
> do it! What is the lightest way of doing this synchronously? I don't mind
> blocking for several seconds as this request is done only once, upon
> initialization.

You can't do it synchronously. Just put all of your main application
code in a function (heck, you can even call it 'main'), and then just
use that as the callback for your https.request.

dhruvbird

unread,
May 31, 2011, 4:52:41 PM5/31/11
to nodejs
I guess it's time to write a cURL wrapper for node.js.
I've always wanted remote-requires:

var foo = require('http://node-modules.org/foo.js');

Don't know if it is a good idea though.

Regards,
-Dhruv.

Isaac Schlueter

unread,
May 31, 2011, 5:33:02 PM5/31/11
to nod...@googlegroups.com
No. This is a terrible idea. Node lacks synchronous HTTP for a
reason. It was a mistake in the browser, and it's a mistake on the
server. HTTP is by its nature async and streaming, which is why it
works so well over high-latency IO like the internet.

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

axs

unread,
May 31, 2011, 5:50:16 PM5/31/11
to nod...@googlegroups.com
I understand the benefits of async network i/o, just wondering if there was some kind of workaround for the *few* cases where sync requests are valid. 

Anyway, I've reworked the code to not need this. Thanks folks.

axs

dhruvbird

unread,
Jun 1, 2011, 12:29:26 AM6/1/11
to nodejs
Yes, I understand that this is a bad idea in *almost* all cases and am
glad that node.js tries to be async for *almost* everything. Just
wondering if the idea of remote requires is a good idea? Considering
you've spent a lot of time with packaging, modules and API for the
same, was wondering if this topic ever came up and what did (you and)
the community in general have to say about it. Asking because I feel I
could really use such a thing. Not that remote requires can not be
done right now, but it's a lot of work to get the file and then
require() it, etc...

Regards,
-Dhruv.

On Jun 1, 2:33 am, Isaac Schlueter <i...@izs.me> wrote:
> No.  This is a terrible idea.  Node lacks synchronous HTTP for a
> reason.  It was a mistake in the browser, and it's a mistake on the
> server.  HTTP is by its nature async and streaming, which is why it
> works so well over high-latency IO like the internet.
>

dhruvbird

unread,
Jun 7, 2011, 5:35:01 PM6/7/11
to nodejs
Hey,
In case you still want it (I've landed up wanting it a few times
after I first replied), here it is: https://github.com/dhruvbird/curl.js

Regards,
-Dhruv.

On May 31, 8:45 am, axs <alexst...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages