JavaScript for VBA Developers: The One-Line Web Server

I’ve been taking a look at server-side JavaScript recently, in particular, Node.js. I’m absolutely blown away by how it simplifies the task of creating your own web server. To prove it, I ‘created’ a one-line web server. Here it is:

require("http").createServer(function (req, res) {require("fs").createReadStream("./index.html").pipe(res);}).listen(3000);

Note: due to its length, WordPress spreads the one line over two but, the above is a single line of code. Let’s take a closer look at it since it demonstrates some of the features of the JavaScript language.

Sry. I had to get that one in there. I just love that imperative, for it says so much about the JavaScript language. In JavaScript, functions are first class citizens. This means you can, inter alia, pass a function as an argument into another function. We can see that above. We’ve passed an anonymous function – function (req, res) { } – into the receiving function ‘createServer’.

In VBA, while you can pass an object and a string into a function and then use the VBA built-in ‘CallByName’ function to call a method on that object, you can’t pass a function directly into another function in VBA. Functions are not first class citizens in VBA.

Method Chaining
There are 2 instances of method chaining at work above. The first is the obvious piping of the output of createReadStream to res, ie:

createReadStream( ).pipe(res);

Note: ‘res’ is the result that is passed back to the web browser – in this case the stream read from the index.html file.

The second is the less obvious binding of the newly created server object to listen on port 3000, ie:

createServer( ).listen(3000);

Here the result of calling ‘createServer’ – ie. the server object that is returned by the call – is having its ‘listen’ method invoked with an argument of 3000 passed through. The VBA equivalent would be:

(Set obj = New Server).listen 3000

I’ve used brackets around the instantiation to indicate that the output of that instantiation (ie. the new Server object) is having its listen method called with an argument of 3000 passed in. Of course, the above is not possible to do in VBA.

You can chain your own objects in VBA, using class composition, to get to a method of the last object in the chain, but this is not the same thing as calling a method, on the output of the immediately previous method.

So, there you have it: the one-line-web-server. So what does it actually do? Well, not a lot, actually. It simply streams the contents of index.html to the web browser that connected to it. That’s it. If you wanted to dumb it down even further you could replace the function body:



res.end('Hello World!');

‘Even simpler. ‘And even more static. But what do you expect for 1 line of code?

Categories:JavaScript, VBA

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: