Download and Unzip a Zip file with Node.js

1. Install a javascript zip implementation. I like adm-zip.

npm install adm-zip

2. Require the node http module

var http = require('http')

3. Require the node file system module (fs)

var fs = require('fs')

5. Construct your temporary file path

var tmpFilePath = "assets/tmp/" + filename + ".zip"

6. Use http to get the url to the zip file, then synchronously append the data to the temporary file path. Use AdmZip to extract the zip to another directory.

7. Delete the zip file

Code and usage:

		var download = function(filename, url) {
			var tmpFilePath = "assets/tmp/" + filename + ".zip"
			http.get(url, function(response) {
		 		response.on('data', function (data) {
		 			fs.appendFileSync(tmpFilePath, data)
				});
		 		response.on('end', function() {
		 			 var zip = new AdmZip(tmpFilePath)
		 			 zip.extractAllTo("assets/extracted/" + filename)
		 			 fs.unlink(tmpFilePath)
			 	})
		 	});
		}

		for (var i = 0; i < objectsToDownload.length; i++) {
			download(objectsToDownload[i].filename, objectsToDownload[i].url)
		 }

502 Bad Gateway on nginx/1.2.6 using Elastic Beanstalk and Node.js

The quick fix for this is for express/http server  to listen on process.env.PORT,  not on a hard coded port (e.g. 3000). This can be accomplished in express by using

var express = require(‘express’);
var app = express();
app.set(‘port’, process.env.PORT || 8081);
…..
app.listen(app.get(‘port’));

 

I could not find any options or commands to configure the port for node in Elastic Beanstalk. At the moment, Nginx on an aws 64 bit linux instance is connecting to port 8081 to the node instance. process.env.PORT is set to 8081. When Express was listening on 3000 the bad gateway error was showing up. The logs/events provided by EB were useful to get to the root of this.

The DrawingBoard

Collaborative online sketchpad that i’ve been working on during my spare time. You can invite a friend to the site and draw/chat together with them. To create a room add ?room=roomnamehere to the URL. Work in progress.

Object Oriented Javascript, HTML5, CSS, Node.js/Express Framework, Socket.io for events and chat, Mongo DB and Amazon s3 for storage., Redis as the backend store for socket.io for scalability.

Give it a try at http://rtimaldraw.jit.su

Source code available at:

https://github.com/RTimal/DrawingBoard

https://github.com/RTimal/Drawingboard-Chat

https://github.com/RTimal/Drawingboard-Drawing

 

drawingboard

Theres are three repos, one for chat, one for the main server(that serves up pages and saves images/users), and one for the drawing events.

 

Unity 3D Project – OrganLearn

Have you ever studied for biology or a&p and wished you could just rotate through organ systems and touch what you wanted to know about? I’m developing an app that allows you to do this.  Touching the name of an organ component takes you to a page with a description of the anatomy and physiology of that component. Just neat, something i’d like to have for my own benefit. Here is a very early pic showing some of my progress.

Simple Javascript to clear input boxes and automatically refill them using focus and blur event listeners

The following code automatically clears form input boxes when they are clicked on. If still empty, they are reset to the original values when they lose focus again. This will not work for textarea tags. This will only work if the id attribute of the input tag is not set or is not being used by anything else like the stylesheet. In this code, the id attribute is used to store an index into an array (results) that contains the original input tag values. This array is referenced whenever a blur or focus event occurs on that input box. There are many, many ways to do this that are more efficient. However, this can just be dropped onto a page at the bottom if the conditions stated above are met. Make sure to add the code after the form is defined in the html.

<script type=”text/javascript”>
//refill forms automagically
//create new array to store original input values var results = new Array();
 //get amount of input tags var inputlength = document.getElementsByTagName(“input”).length;
 //event listener for blur function blur(e){
 //if value is empty if(e.target.value==””){
 //reset to orginal value e.target.value=results[e.target.id];
 //original value is stored in id attribute }
}
 //event listener for focus function focus(e){
 //if value is the original value if(e.target.value==results[e.target.id]){
 //clear it e.target.value=””;
}
}
 //add event listeners to the inputs function addevents(){
 //get all input tags var els=document.getElementsByTagName(“input”);
//els=els.concat(document.getElementsByTagName(“textarea”);
 //loop through all of them for(var i = 0;i<els.length;i++){
 //store the original values in results results[i]=els[i].value;
var el =els[i];
  //add event listeners to the input el.addEventListener(‘blur’,blur,false);
  //add event listeners to the input el.addEventListener(‘focus’,focus,false);
//if its not a submit button, set the id attribute to the index. if(el.type!=”submit”){el.id=i;}
}
}
addevents();
 </script>