How to tell if request was from browser or browser extension - node.js

When I run the following code I get two requests from my Chrome browser and have narrowed it down to one extension just by disabling it in chrome, but my question is how do I tell from my server side code which is the browser request and which is the extensions request
var http = require('http');
var server = http.createServer((request, response) => {
if (request.url == '/favicon.ico'){
console.log('ignore icon request')
}else{
console.log(request.url);
response.write('hi');
response.end();
}
});
server.listen(3000);
calling http://localhost:3000/, my output ends up being
/
/
ignore icon request
so similar to what I did with ignoring the favicon, is there a simple way of ignoring that extension request?

Related

res.end() not closing the script execution in NodeJS

return res.end() not closing script execution.
Initially it renders the form and when i submit it executes the code block below closing brace of if statement.
I think execution should have ended on the line return res.end().
const http = require("http");
const server = http.createServer((req, res) => {
const url = req.url;
if (url === "/") {
res.write("<html>");
res.write("<head><title>FORM</title></head>");
res.write(
"<body><form action='/message' method='POST'><input type='text' name='message' /><button>Send</button></from></body>"
);
res.write("</html>");
return res.end();
}
res.setHeader("Content-Type", "text/html");
res.write("<html>");
res.write("<head><title>APP</title></head>");
res.write("<body><h1>Demo APP</h1></body>");
res.write("</html>");
});
server.listen(4000);
Please add console.log(req.url) to the start of your request handler. You will find that:
return res.end();
does indeed stop the execution of that particular request by returning from the request handler callback.
But, you are most likely then getting another incoming http request, probably for favicon.ico. In general you should never have code in a request handler that pays zero attention to what resource is being requested unless it's sending back an error like a 404 because there are lots of requests that can potentially be made to your http server for somewhat standard resources sucn as robots.txt, favicon.ico, sitemap.xml and so on and you shouldn't be answering for those requests unless you are sending the appropriate data for those requests.
You might want to put the remainder of the code in ELSE block to ensure it is run only when URL!=="/"

Node HTTP Server does not respond to first request within Electron

I'm trying to start a local server in Electron to capture Google's OAuth callback like so:
this.server = http.createServer((request, response) => {
const { query } = url.parse(request.url, true);
if (query.code) {
this.onCodeReceived(query.code);
// do something nicer here eventually
response.end();
} else {
response.end('Error');
this.authStatus = 'error';
}
}).listen(this.LOCAL_SERVER_PORT);
The issue I'm having is that when I finish authenticating with Google the window just sits at "Waiting for 127.0.0.1..." and never actually finishes. I've found using console.log in the request handler that the handler is never actually called, so I'm stumped as to why the request isn't going through.
I've verified that the callback URL has the same port the server is listening to, and that the server actually begins listening. Weirdly if I open a new tab and go to the URI I get the expected Error response.
For reference the callback URI is set as http://127.0.0.1:18363 and this.LOCAL_SERVER_PORT = 18363.
If anyone has any ideas I'd greatly appreciate it. Thank you!

NODE JS http form post failing without fidler proxy

I am making http form post using request node module (https://www.npmjs.org/package/request).
The post request fails with error - {"code":"ECONNRESET","errno":"ECONNRESET","syscall":"read","level":"error","message":"","timestamp":"2014-09-05T17:35:34.616Z"}
If I run fiddler and channel the request via fiddler, it works fine.
Any idea why would that happen and how I can resolve it!
Here is the exact code I am using. It works as long as I use it as stand alone node js. But, if its made a web app, it fails. It seems to work a few times.., but fails continuously after that. At this point, even the stand alone node js fails to run.
However, when I enable the proxy and open fiddler, it works magically!
var request = require('request');
var url = "https://idctestdemo.hostedcc.com/callcenter/mason/agents/pipes/muterecording.pipe"//"https://restmirror.appspot.com/"
,form_data = {"pipe-name": "Deepak"};
var args = { url: url, form: form_data};
if(process.argv.length > 2){
var enable_proxy = process.argv[2];
if(enable_proxy && enable_proxy[0] == 'p'){
console.log('enabling proxy')
args['proxy'] = 'http://127.0.0.1:8888';
args["rejectUnauthorized"] = false;
}
}
var oclient = request.post(args
,function(error, response, body){
if (error){
console.log('error in reponse');
console.error(error);
} else {
console.log('success!');
console.dir(body);
}
});
oclient.on('error',function(err){
console.log('client error');
console.error(err);
});
oclient.on('end',function(end_data){console.log('end', end_data)});
oclient.on('data',function(d){console.log('on data', d)});
console.log('waiting for response...')
If everything goes well, the expected response should be
<script>
parent.xb.pipe.onNotify('Deepak',{desc:'Pipe opened by server',payload:{time:'Mon Sep 8 12:58:18 2014'},type:'MSG_OPENED'});
</script>
<script>
parent.xb.pipe.onNotify('Deepak',{desc:'Not logged in',payload:{hostcode:'ny-1'},type:'ERR_NOLOGIN'});
</script>
<script>
parent.xb.pipe.onNotify('Deepak',{desc:'Pipe closed by server',payload:null,type:'MSG_CLOSED'});
</script>
ECONNRESET
means the other side you want to connect/send data to, is not accepting requests.
Maybe you can find more information by looking into the server log.
You also could try to check what happens if you change the port listen to.
If I should help more, I need more information.

Why are multiple requests being processed for a single browser request?

Note: I am very new to node, and I have a VERY simple node site running based upon this example (http://blog.falafel.com/Blogs/BasemEmara/basem-emara/2014/03/18/getting-started-with-node.js-for-windows)
My code is:
var http = require('http');
var reqCount = 0;
http.createServer(function (req, res) {
reqCount++;
res.writeHead(200, { 'Content-Type': 'text/plain' });
console.log(reqCount);
res.end('Request: ' + reqCount);
}).listen(3000);
In the browser I initially get 1, hit refresh and get 3, 5, 7
And in the console I am getting every int, two per request
Why is this executing twice for each request?
I also know that I will not be handling requests directly, but wanted to start basic and then include express.
This may be /favicon.ico request done by your browser.
You can also print request url from req object to see that:
console.log(req.url);

Node.js: Reading a variable from another module returns wrong value

I'm new to Node.js. I just wrote an http server module, with a count variable that stores number of times the module has received an http request:
var http = require("http");
var count = 0; //no of requests so far
function start() {
function onRequest(request, response) {
console.log(count + ": Request received in Main Server");
count++;
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello! you are request no. " + count);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Main Server has started.");
}
function getCount() {
return count;
}
exports.getCount = getCount;
exports.start = start;
Then I wrote another server, let's call it test.js that starts the server module, but at the same time listens to http requests on another port, let's say 8889. The test.js is supposed to show number of requests that server.js has served so far.
var http = require("http");
var server = require("./server");
server.start();
function onRequest(request, response) {
console.log("Request received in Test Server");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello! Server app has served: " + server.getCount());
response.end();
}
http.createServer(onRequest).listen(8889);
console.log("Test Server has started.");
When I run test.js, and make requests to server.js (http://localhost:8888), it adds up to the count. (I get double requests each time which as I read somewhere is due to the fact that the browser sends another request to get favicon.ico, ok, fine, that is not my problem). My problem is that when I send a request to test.js (http://localhost:8889), I always get number of requests I have already made to server.js plus one extra! In ther words, if http://localhost:8888 shows me 1, http://localhost:8889 which reads the same value from my server module shows 2!
Anyone has a clue why it is like that?
Thanks in advance!
When you hit refresh from a browser, requests are usually(I believe always, in Chrome I know it is always, not as sure in other browsers) made in this order:
yourdomain.com/
Followed by
yourdomain.com/favicon.ico
So, you are displaying the count after the first request. And THEN your favicon is requested, which is incrementing the value of your count. If you're making requests from a browser you will NEVER see the same value in both windows, because your favicon request will always come in, before you are able to request your 8889 port. I guess, it is theoretically possible. If you could hit refresh on both windows within X number of milliseconds, you could ping 8889 before the favicon request, but if you're working from a local machine, this number of milliseconds would be so small as to be impossible.
If you want to validate this you could do a simple check like this:
if(request.url.match(/favicon.ico/i) === false) count++;
Which should keep your count from updating for favicon requests.

Resources