Express: How to get http request’s raw buffer - node.js

My application is running an Express http server. I need to save the whole http request message (headers, body,..) in its raw format just as was sent by the client to the server. I can’t find Where it is stored. Any one?

The whole thing isn't stored anywhere. Express uses the http module in node.js which parses the incoming request and headers into a data structure. The body of the request (like for a POST or a PUT) is left to you to read from the incoming readstream so you can certainly read the exact body yourself. Just hook up a listener for the data event on the incoming request stream and you can read the exact bytes of the body right into a Buffer object.
You could reconstitute the http command line and all the headers if you want from the data structure, but I don't think that original data is stored exactly as it arrived.
Some of the information is in these properties as documented here:
request.rawHeaders
request.method
request.httpVersion
request.headers
request.url
If you can explain exactly what the actual problem is that you're trying to solve, we'd have a better idea exactly how else to help you.

Related

How to know the size of an HTTP request before it reaches its destination?

I am studying NodeJS and I have learned about streams and buffer. I found it very interesting because it occurred to me to use it on an HTTP server, since if a request is too big, the data can be sent little by little, especially when using the verb POST to upload big files for example. The problem is that not all data (files at this case) is very big. Is there a way to know the size of a request before it reaches its destination?
From the receiving end, you can look at the Content-Length header.
It may or may not be present, depending upon the sender. For info about that, see Is the Content-Length header required for a HTTP/1.0 response?.

Send json data (asynchronous) to my response

So,upon request I respond with a basic template(I use ejs) and I want to add json data to it when I get from an external API.
I have noticed that I can't use res.render or res.write twice(I get errors) so I really don't know what to do.Any ideas?
You can only send one response for each request. So you either need to do two requests or combine the data you want to send into one response.

npm request conditional piping based on response

I need to download a file by POSTing to a REST server.
I was first using http when it was get. But now i need to use POST, and node's http post is way too complicated, I dont want to build a low level request, so I dont want to use it.
I am now using request. https://www.npmjs.com/package/request
Now, my server either sends {isUpdateAvailable:false} or it sends a tar file.
So i need to save the file, or show 'Already up to date'. How do I pipe to a filestream by checking the response header?
I need to set the pipe along with the request code, so I'm not able to separate the code properly. I need to fs.createWriteStream only if its necessary. Will it be possible?
So, how do i do it?

ServiceStack - OnEndRequest capturing Response body

I have a RequestLog feature completely decoupled from the application logic.
I capture the request/response in a pre request filter. To acomplish this, I instantiate a request scope object that keeps the request context. And before everything gets disposed (at AppHost's OnEndRequest handler), I write to the db. One line per http req.
I'm able to access the response code, the path, the method, request body, request headers, etc.
However the response stream is not available as it was already disposed. What's the logic behind this? Is it something like, IIS writes the stream content to the wire, and releases the resource imediately? Is there any way I can capture the Response body at the OnEndRequest handler?
Thanks
No, ServiceStack doesn't buffer the Response stream it gets written directly to the ASP.NET response.
You can add a Global Response Filter or Custom ServiceRunner to capture the Services response if the request reaches that far, but the request can be short-circuited at anytime throughout the request pipeline by closing the response.
I had the same issue with capturing the response in ServiceRequestLogger. Read this post - it is solved in 4.0.39+ (currently pre-release)
ServiceStack response filter "short circuiting" causing problems

How to send files with node.js

How do you send files on node.js/express.
I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?
What should the headers be.
What if it's a very large file on a couple of GBs?
Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?
Please give me some information about this.
Thanks!
app.get('/img/bg.png', function(req, res) {
res.sendFile('public/img/background.png')
})
https://expressjs.com/en/api.html#res.sendFile
use "res.sendFile". "res.sendfile" is deprecated.
For large files, you will want to use node.js's concept of piping IO streams together. You want to open the local file for reading, start the HTTP request to rackspace, and then pipe the data events from the file read process to the HTTP request process.
Here's an article on how to do this.
Superagent is fine for small files, but because the superagent API presumes your entire request body is loaded into memory before starting the request, it's not the best approach for large file transfers.
Normally you won't need to worry specifically about the request headers as node's HTTP request library will send the appropriate headers for you. Just make sure you use whatever HTTP method your API requires (probably POST), and it looks like for rackspace you will need to add the X-Auth-Token extra header with your API token as well.
I am using Rackspace Cloudfiles and wanna send images/videos to their remote storage but I am not sure that it's as simple as reading the file (fs.readFileSync()) and send the data in the request body, or is it?
You should never use fs.readFileSync in general. When you use it, or any other method called somethingSync, you block the entire server for the duration of that call. The only acceptable time to make synchronous calls in a node.js program is during startup.
What should the headers be.
See RackSpace Cloud Files API.
Is it possible to use superagent (http://visionmedia.github.com/superagent) for this or is there a better library for sending files?
While I don't have any experience with superagent, I'm sure it will work fine. Just make sure you read the API documentation and make your requests according to their specification.

Resources