gzip compression tool usage - javascript

I just got the GZip software from this site http://www.gzip.org/#intro and was looking at it and tried to convert a javascript file which it just converted directly and changed the extension to gz.Can someone tell me how can we create a new file without modifying the original file using GZip command line and also is this GZip file the same which say a web server like IIS creates and sends to the client when compression is enabled.
I am thinking of GZipping all our JS and CSS and HTMl files before hand so the web server can directly render it .I know the web server by itself only renders these zip fles if the client supports but I m just trying some new stuff.

Assuming you're on a *NIX machine, you can use
gzip -c FILE > FILE.gz
to write the gzipped data to a different file. The -c writes to stdout, and the > redirects stdout to a file. If you have many you could try a loop in Bash:
for file in *.js
do
gzip -c "$file" > "${file}.gz"
done
Also, be really really certain your server falls back on the nongzipped versions if the client doesn't support it!

Related

Downloading text file using curl in HTML

I am trying to download text file from server to local directory.
If I execute following curl command it copies myfile.txt from server and saves in same directory as newfile.txt.:
curl -o newfile.txt http://myserverip/myfile.txt
I want to automate this by running the command from javascript while loading the webpage.
For example if I open an html page (which runs a javascript) like http://myserverip/getnewfile.html in the browser the myfile.txt from the server should be copied to newfile.txt in the loacl directory.
Can anyone help me to write javascript to execute the curl command?
Please note that the server in local area network and router is configured to allow connections only from white-listed mac ids of local machines so there is no any authentication required to connect to server.
You can't run local command-line commands from JavaScript, because that would be a massive security vulnerability. The tool you want to use for this is either window.open (to open a tab with the file in order to cause the browser to download it) or the fetch API (to retrieve the file for use in JavaScript).

Simplest Way To Deploy Javascript Application On Linux

I have an folder say 'mywebapp' on windows machines. This folder has index.html page, js directory with java script files and css directory with css files.
Now when i open this index.html into browser, the browser displays contents pretty well, as if i have deployed this application on server, which is not the case.
Now i wanted to do same on my Linux machine vm, login-ed through putty. I tried using pythons SimpleHTTPServer which gave me same result. But as soon as i exit from putty session, the webpage doesnt display. seems like SimpleHTTPServer server connection is broken once i exit the putty session.
Please help me.
Or any other professional and easy way to get my webpage displayed. Tomcat seems good option but i don't have root permission and don't want hectic deployment process.
I heard about node.js, but i don't have root permission to install node.
Most simplest way i can suggest it to download and copy tar.gz file from location:
https://tomcat.apache.org/download-70.cgi
1 then gunzip and untar this downloaded file.
2 Go to conf/Catelina/localhost folder.
3 create an xml with your application name, e.g. mywebapp.xml
and put following to this file:
<Context path="/mywebapp" reloadable="false" docBase="<root-path of your application folder>"/>
here "root-path of your application folder" will be the root folder of ypur HTML, js and css files.
Then just start this tomcat using /bin/startup.sh command and check on browser using localhost:8080/mywebapp

D3.js: Treemap doesn't load from JSON file

I am trying to run this very example of a treemap on localhost, but I can't load the JSON file (which, by the way, is the same JSON file that the example uses).
The console returns the next error in Google Chrome:
XMLHttpRequest cannot load file:///C:/Users/Usuario/Downloads/d3/flare.json. Cross origin requests are only supported for HTTP.
The JSON file is in the same folder as the html file.
Thanks in advance for your help.
You cannot load local files because of the security policy. To quote the D3 website:
When developing locally, note that your browser may enforce strict permissions for reading files out of the local file system. If you use d3.xhr locally (including d3.json et al.), you must have a local web server. For example, you can run Python's built-in server:
python -m SimpleHTTPServer 8888 &
or for Python 3+
python -m http.server 8888 &
Once this is running, go to http://127.0.0.1:8888/.
If people working d3.js on the xampp or wamp they can run their html file as like php file by starting the server.
I found the same issue then I started the wampp server then the file get loaded successfully without any issue like "XmlHttpRequest Access control allow orgin".
I am working in WAMP. I hope same thing for XAMPP, but I am not sure...

Unexpected token ILLEGAL with gzip compression

I am having a problem with gzip compression on my site. I've combined several javascript files to one files and they work ok without compression. Afterwards I've compressed them (gzip) and tried to run the site again, but get the following error:
Uncaught SyntaxError: Unexpected token ILLEGAL
I've compressed the file using several methods, one using gzip software and I also tried other gzip online compression tools like this one.
I can't understand why I get this error why it doesn't work, because the non-compressed version does work. I ran the site on Chrome, latest version.
It also tells me that the error is in line 1
all_js.js.gz:1
Of course the compressed code has many lines, not just one.
I've read a few answers, but nothing regarding gzip compression.
I get this error when working with Visual Studio (ASP.NET) on local machine.
I tried serving the file from S3 with 'application/x-gzip' headers, but it still didn't work. I want to serve my files via CDN after compressing, so I don't search for server compression solutions.
You cannot compress a javascript file with gzip or any other zip routine and load it into your webpage.
You can do one of two things:
Use a javascript code compression tool like the YUI compressor.
Configure your webserver to compress the files (please read the documentation of your webserver, it should not be very difficult)

How to Gzip a javascript file?

179KB of jQuery is Minified and Gzipped to 26KB. I am trying to do the same compressions to my javascript files and found the Yuicompressor to Minify it. But I am still searching for a way to Gzip it.
How can I Gzip my Javascript files?
If you are using it in a webpage, gzip is a configuration method in your web server. The file is gzipped by the server, sent to the browser. No manual action is needed. For Apache: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html.
If you are delivering the code to developers, you could use the gzip command.
You would use gzip, the GNU compression utility. Luckily, the gzip algorithm and file structure is implemented by numerous other tools, such as 7zip (for Windows). You can configure your server (via mod_deflate or others) to compress files on the fly, but for static content its a waste of CPU power.
Here is an article which shows how to transparently serve pre-compressed gzip to browsers which support it: http://blog.alien109.com/2009/03/17/gzip-your-javascript/
You could also use zbugs.com - just provide your website url and it will do everything for you.

Categories