Monitor changes of remote file without downloading it with nodejs - javascript

Is there a possibility to monitor changes of file on some server without downloading it?
I read about chokidar module but I cant find anything about my issue
I believe there is the way to watch some headers or smth like this
Maybe here is someone who have solved similar issue?

You can check Last-Modified and/or E-tag http headers.
var http = require('http');
var options = {method: 'HEAD', host: 'stackoverflow.com', port: 80, path: '/'};
var req = http.request(options, function(res) {
console.log(res.headers);
}
);
req.end();

Yes you can "watch" files using NodeJS.
How to do it:
There is several NodeJS modules allowing that. Take a look at watchr for example. You can "watch" a file or a directory using it.

Related

Electron doesnt execute https call to local spring server, curl works

I'm simply trying to connect to a local Spring configured with SSL / TLS 1.2.
Context (Server): Server is built with Spring, logs when creating a request via curl. Also requires a certificate from the client (X.509 certificate based authentication)
Client (CURL, command-line):
Works fine!
Client (Electron):
When the code below gets executed nothing happens. No requests are being made to the spring (nothing logs), nor is any error occuring. Nothing. Electron is such a drag to debug..
Code:
let options = {
hostname: 'localhost',
port: 8443,
path: '/',
cert: fs.readFileSync(global.relativePaths.config + 'client.crt'),
key: fs.readFileSync(global.relativePaths.config + 'clientprivate.key'),
passphrase: '[phrase_here]',
rejectUnauthorized: false,
requestCert: true
};
let request = https.request(options);
request.on('error', () => {
console.log("Error!");
})
Also, this is just localhost, hence the rejectUnauthorized - I'm working with self-signed certificates until ready for production. :)
Thank you in advance. :)
EDIT:
using the test-code on the wiki (call to github) outputs with no problem.. what could it be?...
also, im doing this on the main process (not on the renderer)
Try using the http-client library like [axios][1]. axios is a promise-based HTTP client for the browser and node.js. It worths giving a try to most used libraries like axios, it's simple to use. If the issue still persists, try setting the User-Agent header in your request.

Node.- issues when trying to request behind https proxy

I am trying to download an image from a server through an https proxy, please help.
My code:
var request = require('request');
request({
url: url,
proxy: proxy
}, function (err, res, imgBuffer) {
console.log(err)
console.log(res)
})
The error:
Error: tunneling socket could not be established, cause=write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:827:
I can provide any additional info needed, I have already tried a lot.
I'm thinking you need to change the config file in npm.
Check out this tutorial: https://www.jhipster.tech/configuring-a-corporate-proxy/
Go down to the section titled "NPM configuration."

create-react-app exclude/include/change code parts at build

I'm developing a React web app and I'm using the create-react-app npm utility.
My app communicates with a server which, during development, is on my local machine. For this reason all the Ajax request I make use a localhost:port address.
Of course, when I'm going to build and deploy my project in production I need those addresses to change to the production ones.
I am used to the preprocess Grunt plugin flow (https://github.com/jsoverson/grunt-preprocess) which has the possibility to mark parts of code to be excluded, included or changed at build time.
For example:
//#if DEV
const SERVER_PATH = "localhost:8888";
//#endif
//#if !DEV
const SERVER_PATH = "prot://example.com:8888";
//#endif
Do you know if there is a way to do such thing inside the create-react-app development environment?
Thank you in advance!
I'm not too sure exactly how your server-side code handles requests, however you shouldn't have to change your code when deploying to production if you use relative paths in your ajax queries. For example, here's an ajax query that uses a relative path:
$.ajax({
url: "something/getthing/",
dataType: 'json',
success: function ( data ) {
//do a thing
}
});
Hopefully that helps :)
When creating your networkInterface, use the process.env.NODE_ENV to determine what PATH to use.
if (process.env.NODE_ENV !== 'production') {
const SERVER_PATH = "localhost:8888";
}
else {
const SERVER_PATH = "prot://example.com:8888";
}
Your application will automatically detect whether you are in production or development and therefore create the const SERVER_PATH with the correct value for the environment.
According to the docs, the dev server can proxy your requests. You can configure it in your package.json like this:
"proxy": "http://localhost:4000",
Another option is to ask the browser for the current location. It works well when your API and static files are on the same backend, which is common with Node.js and React.
Here you go:
const { protocol, host } = window.location
const endpoint = protocol + host
// fetch(endpoint)

Absolute path using fs in nodejs

I am trying to pipe an http audio stream from my nodejs server:
var streamPath = 'http://127.0.0.1:1485/mystream.mp3';
var stat = fs.statSync(streamPath);
response.writeHead(200, {'Content-Type': 'audio/mpeg','Content-Length': stat.size});
fs.createReadStream(streamPath).pipe(response);
The problem is that fs doesn't like the absolute path and I get the following error:
Error: ENOENT: no such file or directory, stat 'C:\myserver\http:\127.0.0.1:1485\mystream.mp3'
I can't find a way to use the absolute path. Is this even possible using fs?
'http://127.0.0.1:1485/mystream.mp3' is not an absolute path, it's a URL.
Absolute paths are something like /home/x/dir/file.txt on your filesystem.
If you want to get a stream from a URL then you need to use http or request. Or you need to use a path on your filesystem and not a URL if you want to get a file on your filesystem without using the network.
fs.createReadStream can only open local file in your filesystem.
For more details, see:
https://nodejs.org/api/fs.html
To get a file over a network, see:
https://www.npmjs.com/package/request
https://www.npmjs.com/package/request-promise
You are trying to access a remote resource through fs - that is wrong. fs is meant to be used for the Local Filesystem. If you want to access any remote resource you have to use http or https. However if I see things correctly you are trying to access your localhost, which should work.
Your Application is trying to access following file: C:\myserver\http:\127.0.0.1:1485\mystream.mp3 if you look closely that can't work. Your Path is mixed with your local path and a remote source (which is localhost actually). Try to fix your path, that should solve your problem. Keep in mind that fs will only work on your local system.
You should also think about fs.statSync this will block everything else until its finished.
Docs:
fs: https://nodejs.org/api/fs.html
http: https://nodejs.org/api/http.html
https: https://nodejs.org/api/https.html
Regards,
Megajin

Can a proxy (like fiddler) be used with Node.js's ClientRequest

Can node.js be setup to recognize a proxy (like Fiddler for example) and route all ClientRequest's through the proxy?
I am using node on Windows and would like to debug the http requests much like I would using Fiddler for JavaScript in the browser.
Just be clear, I am not trying to create a proxy nor proxy requests received by a server. I want to route requests made by http.request() through a proxy. I would like to use Fiddler to inspect both the request and the response as I would if I was performing the request in a browser.
I find the following to be nifty. The request module reads proxy information from the windows environment variable.
Typing the following in the windows command prompt, will set it for the lifetime of the shell. You just have to run your node app from this shell.
set https_proxy=http://127.0.0.1:8888
set http_proxy=http://127.0.0.1:8888
set NODE_TLS_REJECT_UNAUTHORIZED=0
To route your client-requests via fiddler, alter your options-object like this (ex.: just before you create the http.request):
options.path = 'http://' + options.host + ':' + options.port + options.path;
options.headers.host = options.host;
options.host = '127.0.0.1';
options.port = 8888;
myReq = http.request(options, function (result) {
...
});
If you want to montior outgoing reqeusts from node
you can use the request module
and just set the proxy property in the options, like that
request.post('http://204.145.74.56:3003/test', {
headers :{ 'content-type' : 'application/octet-stream'},
'body' : buf ,
proxy: 'http://127.0.0.1:8888'
}, function() {
//callback
});
8888 is the default port , of fiddler .
process.env.https_proxy = "http://127.0.0.1:8888";
process.env.http_proxy = "http://127.0.0.1:8888";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
Answering my own question: according to https://github.com/joyent/node/issues/1514 the answer is no, but you can use the request module, http://search.npmjs.org/#/request, which does support proxies.
If you want to configure a proxy in the general case, the other answers are right: you need to manually configure that for the library you're using as node intentionally ignores your system proxy settings out of the box.
If however you're simply looking for a fiddler-like HTTP debugging tool for Node.js, I've been working on an open-source project to do this for a little while (with built-in node support) called HTTP Toolkit. It lets you
Open a terminal from the app with one click
Start any node CLI/server/script from that terminal
All the HTTP or HTTPS requests it sends get proxied automatically, so you can see and rewrite everything. No code changes or npm packages necessary.
Here's a demo of it debugging a bunch of NPM, node & browser traffic:
Internally, the way this works is that it injects an extra JS script into started Node processes, which hooks into require() to automatically reconfigure proxy settings for you, for every module which doesn't use the global settings.

Categories

Resources