Node.js

Worldly adventures in Node.js

You may recall I was inspired to write software emulating Enigma and the Bombe. Colin suggested I write it in Node.js. Node.js is some sort of JavaScript runtime thing that is event-driven. This is well out of my comfort zone: I have some learning to do.

Hello World

I made a start with that well-known program, Hello World.

In Node.js, it was remarkably easy: I created a file called helloworld.js containing the following code:

console.log('Hello World');.

To run this file, I typed:

$ node helloworld.js

at the command line to obtain this output:

Hello World.

Success!

Hello World — server edition

There is also a server version. I saved the following code in a file called helloworld_server.js:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

At the command line, I typed:

$ node helloworld_server.js

and it output:

Server running at http://127.0.0.1:3000/.

This looked promising. I opened my browser and navigated to http://127.0.0.1:3000/; I was shown a page displaying:

Hello World.

Another success!

I opened a new command line window while the server was still running, and typed:

$ curl 127.0.0.1:3000,

which output:

Hello World.

More success!

(To quit the server, press ctrl-c.)

Now I just need to learn how to do all sorts of other things, and I’ll have my Enigma and Bombe machines built in no time.

Do you want to reply?

This site uses Akismet to reduce spam. Learn how your comment data is processed.