Embrace next generation Javascript, today!
by Alexandre Strzelewicz -



Introduction

ECMAScript 6 (ES6, often referred to as “Harmony”) is the upcoming sixth major release of the ECMAScript language specification. It's a significant update to the language since ES5.

At Keymetrics we love innovation and moving forward. Javascript has been evolving fast these days and we are glad to announce support for ES6/ES7! We integrated the Babel project into PM2 to make things easier for developers who want to use it in development or production.

Show me some code!

This is how beautiful ES6 is:

#
# lib.es file
#

export class Shape {  
    constructor (id, x, y) {
        this.id = id
        this.move(x, y)
    }

    get info() {
      return `Shape has id ${this.id} and is at ${this.x},${this.y}`
    }

    move (x, y) {
        this.x = x
        this.y = y
    }
}

export class Rectangle extends Shape {  
    constructor (id, x, y, width, height) {
        super(id, x, y)
        this.width  = width
        this.height = height
    }
}
#
# main.es file
#

import { square, diag, Rectangle } from './lib';

var r1 = new Rectangle(1, 0, 0, 100, 100);

r1.move(20, 20);

console.log(r1.info)  

Learn more about ES6 features here

Run it with PM2

We decided to make life easier for server side Javascript developers by embedding BabelJS and making some checks to be sure that you use this new generation Javascript in a simple way!

Make sure you install io.js to support the entire ES6 feature set

First clone this sample ES6 application:

$ git clone https://github.com/keymetrics/babel-pm2.git

Development mode

To run the script in development mode (watch and restart on file change + display logs in realtime):

$ pm2-dev run main.es --next-gen-js

Production mode

To run the script in background, just do:

$ pm2 start main.es --next-gen-js

Or you can start the package.json as we put some behavioral configuration in it:

$ pm2 start package.json

package.json:

[...]
"apps" : [{  
  "script"      : "main.es",  
  "name"        : "es6-pm2",
  "next_gen_js" : true,      // Activate ES6
  "autorestart" : false,     // Do not restart on exit
  "watch"       : true       // Restart when file changes
}]
[...]

You can now easily try other ES6 features by editing these files!

ES6 Features Highlight

Arrow functions

let x = [0,1,2];  
x.map(x => console.log(x * x)); //arrow function  

Default parameters

function findClosestShape(x=0, y=0) {  
  // ...
}

String interpolation

let number = 34;

let str = `This number is ${number}  
           and it is multiline`;

Symbol Export/Import

lib/math.js

export function sum (x, y) { return x + y }  
export var pi = 3.141593  

someApp.js

import * as math from "lib/math"  
console.log("2π = " + math.sum(math.pi, math.pi))  

Classes and inheritence

class Person {  
  constructor(name) {
    this.name = name;
  }
  describe() {
    return 'Person called ' + this.name;
  }
}

// Subclass
class Employee extends Person {  
  constructor(name, title) {
    super(name); 
    this.title = title;
  }
  describe() {
    return super.describe() + ' (' + this.title + ')';
  }
 }

Find out more about ES6 features here

Project supporting ES6/ES7

Trek Router

Trek is a fast HTTP router, inspired by Echo's Router.

import Router from 'trek-router';

let r = new Router();

// static route
r.add('GET', '/folders/files/bolt.gif', () => {});  
// param route
r.add('GET', '/users/:id', () => {});  
// match-any route
r.add('GET', '/books/*', () => {});

let result = r.find('GET', '/users/233')  
// => [handler, params]
// => [()=>{}, [{name: id, value: 233}]]

// Not Found
let result = r.find('GET', '/photos/233')  

Koa

Expressive middleware for node.js using generators via co to make web applications and APIs more enjoyable to write.

var koa = require('koa');  
var app = koa();

// logger

app.use(function *(next){  
  var start = new Date;
  yield next;
  var ms = new Date - start;
  console.log('%s %s - %s', this.method, this.url, ms);
});

// response

app.use(function *(){  
  this.body = 'Hello World';
});

app.listen(3000);  

RuneJS

ECMAScript 7 API Framework with decorations!

import { type, required, Timestamps } from 'rune/orm';

@Timestamps
export class Todo extends BaseModel {  
  @type(String)
  @required
  content;
}

export class TodosController extends BaseController {  
  async index() {
    let todos = await Todo.fetch();
    this.$reply(todos);
  }

  async create() {
    let todo = await Todo.create(this.$params);
    this.$reply(todo);
  }

  async destroy() {
    let todo = await Todo.find(this.$params.id);
    await todo.destroy();
    this.$reply(null, 204);
  }
}

Companies using BabelJS

Also, tons of other companies already use ES6/ES7 with BabelJS