nodejs logging network traffic - node.js

I would like to know if there is any way that I can enable Nodejs so It will logs the requests that are made during request-response phase. I have Angular Service hosted and Server Side Rendering enabled on it and I would like to know what requests does the server is making to generate content for response. Those requests are some api requests, some backend services etc but I just need to know what requests are made during rendering html Angular Rendered output.

You can use grackle_tracking library https://www.getgrackle.com/analytics_and_tracking
It let's you track all traffic to the console and to your database. In addition it also tracks uncaught and caught errors by your application.

Related

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.

How to do automatic API relay in express.js?

I am working on a frontend express.js app and need to request to a lot of apis from client side(browser) to another server. I need to request those apis from server side and send responses to browser, due to cross domain problem. I am now writing every api as a route method in my app to receive requests and re-send recieved data as responses. Because of the number of apis is huge, and rewrite every request is prone to error and hard to maintain, I wonder whether there is an express.js way to relay all requests with their methods and parameters not changed from browser to the other server. For example, if I request to some /api/test route of my server, it should request to /test route of the other server, receive response and respond it to me.
Unfortunately there is no way to change anything of the server which implemented the apis. So I could only do this work in the express.js app.
Thanks.

I'm confused with the meaning of WAS(Web Application Server) and web framework

I think I know what framework is and some famous framework like ruby on rails, spring, and I think I can distinguish between the meaning of web server and web application server.
but I don't know what is different between WAS and framework, for me I think framework is a kind of WAS because framework is doing many dynamic works related with database handling request from web server(Apache or nginx)
I'm confused with the relationship between these two part in Web programming.
Could you explain it?
Basically the framework is only responsible to provide a response to an http request (that includes handling the database as you said). But Rails isn't responsible to open a new thread (or in some implementations, a process) whenever a new http request arrives - this is done by the application server (such as Puma, Webrick, Unicorn etc). This is called concurrency (the ability to serve the app to multiple requests at the same time, in a nutshell) and is purely the job of the app server. Another thing is understanding (and parsing) the http request - Rails doesn't implement http, it receives a ready request from the app server who does implement http.
In ruby land the job of each part is defined by the rack protocol https://rack.github.io/. Rails, as a rack application, simply waits for "something" (the web application server) to 'call' it (with an http request), and it returns to it the response.
So to sum up: the application server needs to handle threading or multi processing to serve http requests to Rails (the app server is basically always listening on some socket for new requests, and provides concurrency either by forking processes, opening new threads or both. It depends on the app server). The app server therefore also needs to understand http (be able to parse an http request) so it can server that to Rails.
Rails, the web framework, only needs to handle an http request and return the response.
for those who want to understand the difference between web server and app server.
refer to What is the difference between application server and web server?

Requests to api on Heroku-hosted NodeJS app not making it to app

I'm hosting a NodeJS+Express+Mongoose app on Heroku and have been struggling to understand why some of my GET requests are serving stale data, after repeated changes to resources using the api. The changes only get reflected through the GET call after about half an hour or if I restart the server.
The stale resource data gets returned for requests from multiple clients, my Angular JS webapp as well as curl. So, I'm guessing it isn't something to do with caching on the client.
Based on the logging it looks like the requests aren't even making it to the Express app routes, so I'm guessing there's some kind of caching that's happening on the server. I don't see this behaviour on my local system, so is it something that can be disabled on Heroku?
Generally, if you are using Express then your request passes through multiple Express Middleware before actually reaching your app routes. You have to see if there is some error in any of those middleware or there is some validation error for the data which you feed in.
I would suggest you to exhaustively test the data which failed on the heroku app on your local app too.

Accessing inbound HTTP headers in meteor?

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 :)

Resources