Accessing inbound HTTP headers in meteor? - node.js

I'm working on an application that relies on data that the browser sends within the HTTP headers (and there's no way around this). This also happens to be my first time working with something node.js based, so it's very likely I'm completely missing something simple!
Basically what I want to be able to do is call a method on the server from the client, and in that method read the HTTP headers that the client sent.

Meteor doesn't yet provide a supported API for serving HTTP from your app. This is intentional: in the not-too-distant future, your app server is likely to not be just a single process directly serving end users, but an arbitrarily parallelizable service behind a proxy tier. So we'll need to provide a supported API for responded to HTTP requests (REST, eg) that continues to work in such a setting.
Are you sure it needs to be HTTP and that you can't just use a Meteor method?
If you really need to accept direct HTTP requests now, take a peek at how packages/accounts-oauth-helper/oauth_server.js uses __meteor_bootstrap__.app to hook into the Connect middleware framework. This will work for now, but we don't promise that Meteor will always be powered by Connect :)

Related

When we use http.createServer() in Node.js

I just started learning Node.js, and I'm wondering when developer should/need to use http.createServer()
My process of thinking is: "When I want to catch data from server I use: http.request and http.get methodes.

When I want to POST on server I will use http.request with POST method options parameter.
I just can’t figure out, when I should use http.createServer() method?


If I work on remote server (server is already exists).
I red documentation: This class is used to create a TCP or IPC server.
w3School says: The http.createServer() method turns your computer into an HTTP server.
If you can, give me comprehensive explanation with real world example.
Thanks
When you use something like http.request(), you're connecting to another HTTP server and requesting data from it. If you wanted to be that server yourself, you would need to use http.createServer() to do that.
For example, a common use case of Node.js is to create an API server that receives HTTP requests from web pages and fetches or manipulates data in a database. It takes the resulting data and sends it as an HTTP response.
The Node.js documentation you read seems to be for net.createServer(). While http.createServer() does create an underlying TCP server, it also accepts a callback for listening for HTTP requests.
See also: https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener

Is there a way to intercept browser calls in node?

My app was hosted in xxx.com, which gets data from yyy.com. All API requests were triggered from client side.
Is there a way to intercept its request or response in node?
No, and Yes.
For the requests made by your client, you must have some control of the data sent back to the client in order to intercept it.
Assume a scenario where:
Client -----(request)----->Third Party App Server -------(response)-----> Client
In this case, as the back-end server never had a chance to come into picture, there is no way the server can change the data. Well of course, that is when the server doesn't come into picture.
Instead, if you send the request to the node server itself, which forwards the request to the Third Party App server, you obviously have control of the response receive and thus, you can manipulate both request and response or maybe just log it (whatever is your use case).
Client -----(request)----->NODE_SERVER---->Third Party App Server -------(response)-----> Node_Server ----> Client
What a few developers do to intercept the requests made from the client is that they write some client-side JavaScript code and embed it into the browser (Some sort of authentication).
While this works okay in case of normal requests, a person with malicious intents might just disable your front-end interception code and directly receive a response from the Third Party application.
Thus, if you really need to have access to the requests and response,
YOU MUST FORWARD THE REQUESTS TO AN APP SERVER YOU HAVE CONTROL TO.
P.S. It is not just about nodejs.

node.js built in support for handling requests for same data

In my node.js server app I'm providing a service to my js client that performs some handling of remote api's.
It might very well be possible that two different clients request the same information. Say client 1 requests information, then before client 1's request is fully handled (remote api's didn't returns their response yet) client 2 is requesting the same data. What I'd want to is to wait for client 1 data to be ready and then write it to both client 1 and 2.
This seems to me like a very common issue and I was wondering if there was any library or built-in support in connect or express that supports this issue.
You might not want to use HTTP for providing the data to the client. Reasons:
If the remote API is taking a lot of time to process you will risk the client request to timeout, or the browser to repeat the request.
You will have to share some state between requests which is not a good practice.
Have a look at websockets (socket.io would be a place to start). With them you can push data from the server to the client. In your scenario, clients will perform the request to the server, which will return 202 and when the remote API will respond, the server will push the data to the clients using websockets.

Node.js pipelining HTTP client agent?

The HTTP client built into Node.js doesn't seem to support pipelining requests. However, it occurred to me that it may be possible to create an Agent that sets up pipelining in the background. There may be issues getting the response data back the way it should be, but perhaps the Agent could fake out some socket objects to make the HTTP client work like normal?
Has this been done? Alternatively, is there an alternative HTTP client that is a drop-in replacement for the main that supports pipelining? (Ultimately, I'd like to use it with the AWS SDK, so it needs to be compatible.)

Node.js, chunked encoding, and persistent/reusable sockets

I'm building an application that talks to a vendor API. The API expects chunked encoding connections POST over https (and talks XML but that's irrelevant). They also recommend to multiplex requests over one socket connection (not doing so has security limitations as described below).
Node's core https works to the extent that I can establish one connection with the remote web service. I just set 'Transfer-Encoding': 'chunked' header. The problems start when I make a second request to cancel the first request at the remote web service API. The second request comes in on a separate connection and as such is not authorized to affect the conditions set in the first request – only requests that would be on the same socket would be authorized to do so.
What are my options to make this happen with Node.js? I've been looking at Mikeal Rogers' request library, but have not been having luck with it thus far.
Any ideas what route I could go? Many thanks for any insights!

Resources