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...