Mikael's blog

A developers seventh time trying to maintain a blog

Tag #nodejs

New blog on the way | April 15, 2022 at 19:57

I've spent the last week in the mountains, skiing, relaxing and eating well. I also took some time rewriting my blog. The old blog relies on client certificates to enter the admin area. This worked like a charm while I had the certificates on hand, but now, 8 years, 4 computers and a whole lot of reinstalls later, they are nowhere to be found. The new blog is written using Svelte (because why not?) and currently only lives on my laptop. I've written a new admin UI and established a proxy connection to my CouchDB instance through a SSH tunnel. All in all, I've gotten to the point where I can write new posts. (Yay!!) The new blog won't be online for a while though since I need to write a real proxy backend for the database and set up some proper authentication for the admin UI first.

Async File Uploads in HTML5 | May 1, 2013 at 20:30

Uploading files using HTML forms has always felt a bit off for me. You had to set your encoding to `multipart/form-data` and the synchronous nature of form posts always made it a waiting game when uploading larger files. Then came AJAX and the dawn of "single page applications" and such buzzwords, but file uploads somehow got left behind. Javascript didn't have the access it needed to send files with the `XMLHttpRequest`. Years passed and along came the File API and XMLHttpRequest Level 2 (it seems to be called that again) with its `upload` attribute, support for byte streams and progress events. Today I'm going to show you how to build an asynchronous file uploader with it. We'll start with the HTML part: ```html <input type="file" id="files-upload" multiple /> <ul id="file-list"></u

LESS Is More, More Or Less | March 8, 2012 at 23:05

A while back I read a blog post somewhere about how the [LESS](http://lesscss.org/) parser/compiler had been remade in Javascript. "Well awesome", I thought to myself as I had been wanting some more flexibility in CSS but had been to stubborn/proud to install the SASS compiler since it's written in Ruby. Needles to say, I wanted to incorporate it in my blog as soon as possible but I've not had the time to actually do it until now. #### LESS you say? LESS (like SASS) is a CSS derived language that adds a whole lot of long needed features to CSS to ease maintenance of large style sheets. It compiles into regular CSS markup either in realtime (through their nifty Javascript implementation in the browser) or, as in my case, as a bootstrapping task when I start my blog. For now, it's tacked

Cache Me If You Can | March 6, 2012 at 22:30

Today at work was "do-anything-but-work-day". It's a bit like Googles 20%, but instead of 20% it's more like <del>.0001%</del> <ins>.8%</ins> or something like that. It's was our first time and not that many people had a clear idea about what to do at first. I on the other hand had a mission all planned out. #### The Performance Degradation When I put the blog on the new server back in January, I noticed a [small decrease](http://lofjard.se/post/say-welcome-to-ibs) in performance. After a few tests I've realized that the CPU is the culprit. The Atom D525, while dual-core, at 1.6 GHz has roughly half the computational power of the Pentium M at 1.5 GHz, which was what my old server had under the hood. Node.js can make use to multi-core processors by starting more instances of itself, wh

The Development Environment Enigma | March 3, 2012 at 10:22

As you might have noticed, there hasn't been much work done on the blog these last few months. It kind of boils down to complexity. ## The Old Setup When I started building this blog my main workstation was running Windows 7. Everything ran as well on Node.js on Windows as it did on my Linux server. It was a nice and simple setup; develop locally, test locally, deploy on server. ## Enter CouchDB Then I added a database. Specifically CouchDB that only worked on Linux. This meant a new, more complex development routine; develop locally, deploy on server, test on server, rince and repeat in case of error. This worked for a while but lately as the code has gotten more complex and it does a lot of pre-caching on startup, I've been longing for a locally deployed test version again. ## The

Content-Length - A HTTP Header/UTF-8 Story | January 27, 2012 at 14:01

The HTTP protocol has a lot of header fields that affects requests and responses. HTTP also have a couple of different request types (HEAD, GET, POST, PUT and DELETE). Unless you're building a <abbr title="Representational State Transfer">REST</abbr> service, you mostly have to deal with GET and POST on the web, and I don't even differentiate those as much as I should. A couple of weeks ago, a thought occurred to me; "What happens when I make a HEAD request to my blog?". Well the answer turned out to be pretty simple. Node.js ignores any calls made to the <code>write</code> method of the response object if the request was a HEAD request. That's all fine with me, but then I started thinking about what type of things should go into a HEAD response and if I could optimize anything. This lea

The Gift of Source Code | December 21, 2011 at 16:00

I'm not sure if anyone is interested in my source code for this blog, but since I've already posted some bits and pieces of it from time to time, I thought, why not release it all? If your browser has a width of more than 720 pixels, you will see a new button in the menu labeled "Source code". Feel free to browse, and enjoy! DISCLAIMER: Not all of my code is that pretty and I've been meaning to refactor some of it. In other news, I'm leaving for the west coast tomorrow morning, so this might be my last post this year. It all really boils down to how tired I will be after playing with all my lovely nephews and nieces all weekend. Merry Christmas and Happy New Year!

Handling Views in node.js | December 3, 2011 at 21:46

This weekend, my wife and daughter have been away up north, visiting my in-laws. I thought this would be a great opportunity for me to buffer up on some blog posts, but it seems like my inspiration mostly left with them. Last Tuesday at my weekly tutorial at work we did a bit of work with jQuery and Mustache. On my blog I use Mustache, not only on the client side, but on the server side as well. As I enjoy using the MVC pattern, I have tried to implement it to the best of my extent. ## First things first Mustache is a light weight templating engine with emphasis on simplicity. It's been implemented for a lot of languages and one of them is JavaScript. In Mustache, a template looks like this: ````html Hello, my name is {{name}}! ```` In my case the templates involve a lot of HTML, bu

Automatic Minification and Bundling with node.js | November 29, 2011 at 21:51

Scott Guthrie recently wrote about the new [minification and bundling](http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx) process that has been built for ASP.Net 4.5. I read his blog post, liked what I read and then I though of doing the same thing for my blog. I've been looking at minification programs for a while but I've put it off so far because I didn't want to add another step to my manual deployment process. Now I'm thinking I don't have to have another step. I could do it "automagically". ## The BundleController ````js /***************************************** * Bundle Controller ***************************************** * Author: mikael.lofjard@gmail.com * Website: http://lofjard.se * License: MIT Licen

A Simple Routing Solution with Node.js | November 25, 2011 at 22:27

A few days ago I wrote about [not reinventing the wheel](http://lofjard.se/post/why-node-static-is-a-good-idea). Today I'm going to talk about the opposite. If you're building an 'enterprise' grade application, reinventing the wheel is almost always the wrong thing to do. This blog however is something I do just for fun, and reinventing the wheel can sometimes be the best way to learn how stuff works. It's one of the reasons [Ayende](http://ayende.com/blog) had for writing [yet another document database](http://ravendb.net/), and it's the reason why I decided to write my own routing engine. Sure enough I could have used the very popular [Express](http://expressjs.com/) framework, but where's the fun in that? ## What I wanted As you might know, I work almost exclusively with the .Net st

Node.js Development Server Strategy | November 24, 2011 at 17:28

When I started building this blog my server was the production environment and my laptop was the development environment. This all changed when I added the database and started doing more UNIX specific stuff such as path resolving and such. So from then until yesterday my production environment was also my development environment. Not the best strategy I admit. Yesterday when I rewrote the architecture I also decided that I needed to set up a separate node.js instance as a development environment on the server. The first problem I had to solve was configuration. My configuration was scattered all over the code base so I moved it all into `index.js` which is the file I execute with node when I start the server. I also did not want to have to change my configuration before I deploy (copy)

Why node-static is a good idea | November 22, 2011 at 21:40

If you are using node.js as a web server (as I am) and you want to serve up static files there are a lot of ways you can go about doing that. I'm going to talk about 3 of them. ## The bad path: Serving them yourself It's really easy to do this for a proof of concept. Node.js has an easy to use `fs` library for reading and writing files to the file system. In order to serve up some HTML page you just parse the request URL, retrieve the file using `fs.readFile(...)` and then write the file to the response stream. I did this for the small server I use at my Tuesday tutorials at work. The problem we faced last Tuesday was that our web site would not load properly in IE9 without resorting to "compatibility mode". At the time I didn't have time to look into it (as I was tutoring) so I did it

Node TCP Client | November 22, 2011 at 00:38

A few weeks ago I bought an [iTach IP2IR](http://www.globalcache.com/products/itach/ip2irspecs/) from a company called Global Caché. Combined with an iPhone app called iRule it lets me use my iPhone to control my home theater equipment. This has been an awesome experience. No more fighting over remotes since my wife has the same app on her phone, and when our daughter wakes up on the weekends we can turn the TV on in the living room without leaving the bed and thus buy our selfs a couple of minutes more snooze time. The thing that makes Global Caché more awesome than most hardware companies is that they publish their API specs on their web site, and it's a really nice, simple ASCII based TCP API. The API is really what made me choose this product over similar options. Since the API work

The power of CommonJS | November 16, 2011 at 22:44

A few days ago when I was putting my posts into CouchDB, instead of relying on a static HTML file, I also implemented templating with [Mustache](http://mustache.github.com/). Mustache is small, easy to use and has almost no advanced features. It's power lies in the vast amount of implementations it has for different platforms but most of the power comes from one single implementation; the JavaScript implementation. There's nothing really special about the JavaScript implementation except that it is written in JavaScript and is intended to run in the web browser. The implication of having this implementation however is that you can now use the same template files on both the server side and client side. That was a big win for me so I decided to use it. There is also, on [their website](ht

Double linked paging in CouchDB | November 14, 2011 at 23:56

Yesterday I said that I would look into paging, as my post count had reached 10+, so that's what I did today. Paging in CouchDB isn't all that straight forward for a bunch of reasons that I'll try to explain. Firstly, to be able to query anything in Couch you need a View. A Couch View is basically a really fast hash of your documents that is constructed with a little piece of javascript (like everything in Couch). In my case it looks like this: ````js function(doc) { if(doc.type === "blogpost" && doc.published) { emit(doc.dateTime, doc); } } ```` The execution of this code is what makes Couch so fast for reads. The magic can be read about [here](http://couchdb.apache.org/docs/overview.html) and if you're interested in how Couch maintains its indexes, then it's a good read. It

Notes from &Oslash;redev | November 13, 2011 at 17:29

As you know, I went to the &Oslash;redev conference in Malm&ouml;, Sweden last week. It was a great conference and I saw a lot of great speakers talk on a lot of great topics. The first two days of &Oslash;redev were "workshop" days. On monday we had a full day workshop with [Greg Young](http://codebetter.com/gregyoung/) teaching us the ins and outs of [CQRS](http://cqrs.wordpress.com/). The prerequisite to this workshop was a 6.5 hour long [video](http://cqrs.wordpress.com/video/). Between that and the 8 hour long workshop there was a lot of learning going on on the topic of CQRS, Event Sourcing and DDD in general. I always enjoy watching Greg speak because he's very passionate about everything he does and that always gets me going. Tuesday was NServiceBus day. [Andreas &Ouml;hlund](htt

Experimental server is up | November 4, 2011 at 20:52

I've put up a node test server running on port 1337 (it was already forwarded to my server for reasons forgotten by me) and I configured [node-static](https://github.com/cloudhead/node-static) to take care of all my static files. If you want give it a go, head to http://www.lofjard.se:1337 (EDIT: not available anymore) and have a look at an exact copy of this site running on another web server. Setting up node-static turned out to be pretty simple. Example stolen from node-static's GitHub page: ````js var static = require('node-static'); // // Create a node-static server instance to serve the './public' folder // var file = new(static.Server)('./public'); require('http').createServer(function (request, response) { request.addListener('end', function () { // // Serve files!

Sorry, sharing is not available as a feature in your browser.

You can share the link to the page if you want!

URL