Skip to content

Instantly share code, notes, and snippets.

@alansparrow
alansparrow / port.js
Created April 7, 2015 11:01
kill / remove / delete a port
sudo kill $(sudo lsof -t -i:9001)
sudo kill `sudo lsof -t -i:9001`
@alansparrow
alansparrow / 64baseimg.js
Created March 25, 2015 01:14
Get 64 based img
var i64 = require('img-to-64');
i64.getImageStrings({
files: ['./1.jpg'],
css: true
}, function(err, b64strings) {
if (err) {
console.log(err);
return;
}
<script>
function h(e) {
$(e).css({'height':'auto','overflow-y':'hidden'}).height(e.scrollHeight);
}
$('textarea').each(function () {
h(this);
}).on('input', function () {
h(this);
});

Running redis using upstart on Ubuntu

I've been trying to understand how to setup systems from the ground up on Ubuntu. I just installed redis onto the box and here's how I did it and some things to look out for.

To install:

How to set up stress-free SSL on an OS X development machine

One of the best ways to reduce complexity (read: stress) in web development is to minimize the differences between your development and production environments. After being frustrated by attempts to unify the approach to SSL on my local machine and in production, I searched for a workflow that would make the protocol invisible to me between all environments.

Most workflows make the following compromises:

  • Use HTTPS in production but HTTP locally. This is annoying because it makes the environments inconsistent, and the protocol choices leak up into the stack. For example, your web application needs to understand the underlying protocol when using the secure flag for cookies. If you don't get this right, your HTTP development server won't be able to read the cookies it writes, or worse, your HTTPS production server could pass sensitive cookies over an insecure connection.

  • Use production SSL certificates locally. This is annoying

@alansparrow
alansparrow / next(err).js
Created December 23, 2014 11:48
next(err) express
http://stackoverflow.com/questions/13133071/express-next-function-what-is-it-really-for
So what does next do? Simple, it tells your app to run the next middleware. But what happens when you pass something to next? Express will abort the current stack and will run all the middleware that has 4 parameters.
function (err, req, res, next) {}
This middleware is used to process any errors. I like to do the following:
next({ type: 'database', error: 'datacenter blew up' });
@alansparrow
alansparrow / curl.js
Created December 23, 2014 02:21
curl
1. GET with json/xml
curl -i -H 'Accept: application/xml' http://tobi:ferret@127.0.0.1:3000/api/entries
2. POST with form
curl --data-urlencode "entry[title]=Hello Fucking World" --data-urlencode "entry[body]=This Is Awesome" http://baotrungtn:123123@localhost:9000/api/entry
@alansparrow
alansparrow / session.js
Last active August 29, 2015 14:11
Note about session in Nodejs Express
Only a piece of cookie (as a string of char) is stored on client.
Everything else will be stored on Server.
Therefore, every time a client send its cookie (passport) to server, server will retrieve
corresponding data (session).
This data can be stored on RAM or pesisted (Redis).
Ex:
req.session.uid
@alansparrow
alansparrow / tinnhiem
Created December 4, 2014 20:32
tinnhiem
tinnhiem.vn
@alansparrow
alansparrow / weatherjson.java
Created December 1, 2014 09:24
json weather
private class OpenWeather extends Thread {
private String cityID = "";
public OpenWeather(String cityID) {
this.cityID = cityID;
}
public void run() {
// create HttpClient