Tagnode.js

Stubbing middleware when testing an Express with Supertest

S

When testing a simple express app / api I like to use supertest. This can feel like integration testing, and to an extent it is. But we can stub things out with sinon, and tidy up our tests. Let’s look at an example. Suppose we have a simple API with the following router: var express = require('express'), app = express(); var router = express.Router(); app.use('/', require('./router'));...

Transparent proxy from node.js app to WordPress for /blog route

T

For a recent car wash directory start up I have created, I wanted a blog. GetCarClean uses node.js; While there are node blogging platforms, what I really wanted to do, was proxy /blog of my main site, to a WordPress site (actually hosted on a subdomain – blog.<domain>.com) What I did, was to set up the WordPress blog on the separate server, ensuring my styles etc… are in my...

How to assert.throws on an async function with a callback

H

Recently, I was writing a module that took a callback, and needed to write a test, asserting on an exception. Here’s how to do it: it('throws error if myParam is less than 10', function(done) { var fn = function(){ myModule.doSomething(8, function(err) { if (err) throw err }); } assert.throws( function() { fn() }, /Value is less than 10/ ) done(); }); Assuming our module looks something like...

Create a standalone EXE to run a Node.js application

C

Download Advanced BAT to EXE converter () Download Node.exe from Joyent Now we’re ready to bundle the node.js executable and our js files into a single exe file. As for our BAT command, all we need is: CD %MYFILES%/ node test.js %MYFILES% is the variable given to the location of embedded files (more on that in a sec) Your Advanced BAT to EXE converter window should look like the image on the left...

Node RailwayJS app – Deploying to Heroku – ENOENT, open ‘/app/log/production.log

N

I’m currently working on a project involving a node.js app, using RailwayJS, deploying to Heroku. After doing my git push Heroku Everything seemed ok, however, the app was not running. I ran Heroku logs and was shown: app[web.1]: listening on port 32168 within production environment app[web.1]: app[web.1]: events.js:48 app[web.1]: throw arguments[1]; // Unhandled ‘error’ event...

Running node.js on Windows

R

Download node.exe At the time of writing, the version was  v0.5.4 (unstable) Don’t worry about it being unstable, you wouldn’t dream of running it on Windows in production, would you Put it somewhere sensible For me, I have node.exe on the root of C:\ Some people have a subfolder, such as “tools” where they’ve got Ruby installed (incidently, I also have this installed on the root of C:\) Sort...