Response body in Tobi.js? - node.js

I'm using https://github.com/LearnBoost/tobi, a browser simulator, to test an external web app. I'd like to be able to see the response body returned in the callback.
I understand the normal way node.js server-side apps show body is via res.on('data'), but my understanding is that inside the toby browser callback the response is now complete. Yet I can't find a res.body, res.data, or anything similar. Nor can I find docs on the topic!
function(error, response, $){
// Headers are there
console.log(response.headers;
// Horrible hack to show body via jquery as response.body and response.data are undefined
console.log($('body').html());
}
Per the above, I can see the document data via jQuery, so it's there. I just can't find it inside response...

I think your jQuery usage is the intended way to use tobi. It consumes the response and gives you a $ to manipulate or examine it.

Related

how to modify response status code in chrome extension?

using chrome.webRequest I can modify request or response headers, however I also need modify Response.statusCode, is it possible? I do not see anything in documentation.
https://developer.chrome.com/docs/extensions/reference/webRequest/
I see something interesting here:
https://tweak-extension.com/blog/how-to-override-http-status-code#lets-override
It seems the request is not sent but a mock response has been applied but I'm not sure how.

NodeJS + EJS + Promise : page is not displayed until the promise is fulfilled

I retrieve data from a web service in a promise and display it using EJS. The problem is that the HTTP page is not displayed until the promise is fulfilled which makes sense since I call render inside "then".
Is there a way to have the HTTP page displayed without "data" and the "data" displayed when the webService call completes?
var webService = new webService();
webService.GetData("https://somewebservice.com")
.then( (result) =>
{
let options: Object =
{
"data": result
};
this.render(req, res, "index", options);
});
So this might mean you have to restructure your code. But there is something called an observable. It's like a promise, but you subscribe to it which means that after you make the call the callback function is always monitoring it and updating whenever something is returned. It would require a restructure but here is a link to a tutorial.
It would also be good to note that your code will probably need to be restructured anyway, the page will need to load and then make a call for the data if that's the way you want to handle it.
Let me know if this helps.
https://medium.com/#benlesh/learning-observable-by-building-observable-d5da57405d87
The solution I ended up with is:
The page has an onLoad handler on <body> that calls a JSON API on my server using '$.getJSON'
The JSON API calls the web service using promises and in the last 'then' sets 'res.json'
The onLoad handler upon API response updates the HTTP element by setting its text.
This way the page is loaded and displayed then the onLoad handler fires up and deals with the web service and updates the page.
However I would have liked all of this to be transparent, it would have been great if EJS could take care of all of that through a simple assignment.

node.js - express - render multiple views

I'ld like to send two successive ejs page to the client using the following code:
app.post('/', function(req,res) {
res.render('yourDemandIsBeingProceed.ejs');
//some code which require time (open an external programm, run a script, edit a response)
res.render('hereIsYourResult.ejs');
res.end();
});
So, once once the client post his form, he receives a page asking him to wait for a few seconds and then the page containing the response is send.
any suggestion?
many thx
What you can do is have client-side code in yourDemandIsBeingProceed.ejs when the page loads that performs a get/post request to a different endpoint. When the result from that is received, hide/show different elements inside yourDemandIsBeingProceed.ejs so it looks more like hereIsYourResult.ejs
What you really should do is have the user submit information via front end AJAX and put up a loading graphic until the JSON response gets back. AJAX was developed for situations exactly like this.

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response.
I'm using KoaJS. From what i understand, Koajs merges the request and response object into ctx but when reading through the docs I can't find anything along the lines of ctx.send or similar to send a http response.
Can anyone give me some direction or links.
Thanks.
To send the body of a response, you can simply do ctx.response.body = 'Hello'. There are many aliases attached to ctx, so you don't necessarily have to reference the response or request yourself. Doing ctx.body = 'Hello' would be the same as the code above.
If you wanted to set headers, you would use the ctx.set() method. For example: ctx.set('Content-Type', 'text/plain').
To access the query parameters, you would use ctx.request.query['some-key'] (or simply the alias ctx.query['some-key']).
All of the different request/response methods are documented pretty well at the Koa website along with a list of aliases attached to ctx. I highly recommend you give it a read.

how to send just a 200 response in express.js

I have node.js 5.2.0, express 4.2.0 and formidable 1.0.17.
I created a simple form to save a textfield and a photo. It works fine, but the problem is, after the data are uploaded, I can see in the console that the POST is not finished, its still Pending.
In order to finish it I added this to my code
form.on('end', function() {
res.writeHead(200, {'Content-Type': 'text/plain'});
});
I just want to send the headers and nothing on the page. I want the system to get a 200 ok response without having to print anything on the page. But the POST is still pending.
How do I fix this, without having to print anything? What kind of headers I have to send?
Thanks
UPDATE
If I do
form.on('end', function() {
res.end();
});
The POST finishes normally, but I get a blank page. Why is that? I just want to upload some stuff, not print anything on the page, not redirect, stay in the same page.
Thanks again
Try this instead:
res.sendStatus(200);
Or if you want to continue using explicitly defined headers, I believe res.end() needs to be called at some point. You can see how res.end() is utilized in the Formidable example.
The blank page is most likely the result of your client-side form handling. You may want to override the form's submit method and manually post to your express service to prevent the automated redirection you are seeing. Here are some other stackoverflow responses to a question involving form redirection. The answers are jQuery specific, but the basic idea will remain the same.

Resources