Author Archive

[nodejs] Node.js one-liner

November 23, 2015 Leave a comment

You want to execute a Node.js snippet in the command-line non-interactively. Why? Maybe you want to include it in a Bash script.


$ node -p '"jabba".split("").reverse().join("")'

The option “-p” means “print result of --eval“.

Same thing in Python

$ python2 -c "print 'abc'.upper()"

# or, it can be multiple lines too
$ python2 -c "
for x in range(5):
    print x
print 'Finished'

The option “-c” means “command”.

Categories: nodejs, python Tags:

[nodejs] create a TCP time server

November 23, 2015 Leave a comment

I found this exercise at and here is my solution. Write a TCP server that listens on the port provided by the first argument to your program. For each connection you must write the current date & 24 hour time in this format: “YYYY-MM-DD HH:mm” (example: “2015-11-23 16:11”).

We will use moment.js to suck less with dates and times.

#!/usr/bin/env node

"use strict";

var net = require('net');
var moment = require('moment');    // install with npm

const port = process.argv[2];

function main() {
    console.log("The server is listening on port " + port + "...");
    var server = net.createServer(function (socket) {
        socket.end(moment().format('YYYY-MM-DD HH:mm') + "\n");


Start it and the server will listen to incoming connections. When a request is processed, it doesn’t stop. It continues listening for the next connection.

Test it
Start the server and run the following command in another terminal:

$ nc 8000
2015-11-23 16:21

The command “nc” is the netcat command. Under Manjaro it’s in the gnu-netcat package.

Categories: nodejs Tags: , , , ,

[nodejs] working with big integers

November 15, 2015 Leave a comment

Under Python it’s very easy to work with big integers (called ‘long’ type in Python 2). The programmer doesn’t have to worry about the overflow error, the interpreter takes care of it. For instance, let’s calculate 2128:

>>> 2 ** 128

In JavaScript there is just a ‘number’ type, which is floating point. Actually, there is no integer type in JS :) And big integers are not supported, so you need a library if you want to work with huge numbers.

There is a very nice package called big-integer. It has an excellent documentation with lots of examples. Its usage is quite simple, similar to Java:

#!/usr/bin/env node

"use strict";

var bigInt = require("big-integer");

 * 2 ** 128 == 340282366920938463463374607431768211456

function main() {

Categories: javascript, nodejs Tags: ,

[nodejs] raw_input in Node.js

November 7, 2015 Leave a comment

How to read in Node.js from the console? For instance, how to rewrite the following Python script?

n1 = int(raw_input("1st number: "))
n2 = int(raw_input("2nd number: "))

print "The sum is:", n1+n2


#!/usr/bin/env node

"use strict";

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout

function processNums(n1, n2) {
    n1 = Number(n1);
    n2 = Number(n2);
    console.log("Their sum: " + (n1 + n2));

function start() {
    rl.question('1st number: ', function (x) {
        rl.question('2nd number: ', function (y) {

            processNums(x, y);

function main() {


Tip from here.

Categories: nodejs Tags: , ,

Dive into JavaScript

November 7, 2015 2 comments

I’ve decided to dive into JavaScript. I had some superficial knowledge of it, but nothing serious. I’ve used Flask for my pet projects, but they should also look nice on the client side. So expect some JavaScript and Node.js related posts in the future. But fear not, my favourite programming language is still Python :)


Categories: javascript, nodejs

endswith also accepts a tuple

November 7, 2015 1 comment

Have you ever written something like this?

fname = 'movie.avi'
if fname.endswith('avi') or fname.endswith('mp4'):
    print("It's a movie.")

The function endswith also accepts a tuple. Just saying.

fname = 'movie.avi'
if fname.endswith(('avi', 'mp4')):
    print("It's a movie.")

Meaning: if it ends as ‘aviormp4‘, then…

This also works with startswith, of course.

Thanks to one of my students, Marton Sz. who solved one of his exercises using this trick.

Categories: python Tags: , , ,

automatic text summarization

November 2, 2015 Leave a comment

See . In the README there is a list of alternative projects.

Categories: python Tags: ,

Get every new post delivered to your Inbox.

Join 103 other followers