Send json data (asynchronous) to my response - node.js

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.

Related

How to send get request with a body?

I am trying to send a get request to my API to get a list of users. But I need there is an exclude list that the response must exclude. How can I send this exclude list in my GET request?
You can send a body with the request. Query parameters is probably the best way to do it though. The folks at Elastic.co say:
The truth is that RFC 7231—the RFC that deals with HTTP semantics and
content—​does not define what should happen to a GET request with a
body! As a result, some HTTP servers allow it, and some—​especially
caching proxies—​don’t.
The authors of Elasticsearch prefer using GET for a search request
because they feel that it describes the action—​retrieving
information—​better than the POST verb. However, because GET with a
request body is not universally supported, the search API also accepts
POST requests:
You cannot send a request body when making a GET request. However, you can add it as a query parameter. Alternatively, you can make a POST request.

What are the fast request get url library's in nodejs?

I want to send get and post requests to an external site. With which library can I get the returned json data fastest?

QR code best approach for POST request from REST API

I'm setting up a website that will be mobile focused and one of the features I wan't to implement is users to be able to log an entry by just scanning a QR code.
For what I read is not really possible to make a POST request directly from a QR code, so I was thinking in two different options:
1. Make a GET request and then redirect that inside my server to a POST route in my routes.
So the URL would be something like https://example.com/user/resources/someresourceid123/logs/new and then this would create a POST request to https://example.com/user/resources/someresourceid123/logs/ and create the new entry to then send a response to the user but I'm not really sure this is the best approach or if it's possible at all.
My POST request only requires the resourceid which I should be able to get from req.params and the userid which I get from my req.user.
2. Do my logic and log the entry to my DB using the GET request to https://example.com/user/resources/someresourceid123/logs/new.
This would mean that my controller for that request will do everything needed from the GET request without having to make an additional POST request afterwards. I should be able to get both the resourceid and userid from the req object but not sure if being a GET request limits what I can do with it.
If any of those are possible, which would be the best approach?
I'd propose to go with a second option simply for the sake of performance. But you need to make sure your requests are not cached by any proxy, which is usually the case with GET requests.

Express: How to get http request’s raw buffer

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.

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