To just think out of the express generator template and to understand the ES6 mode of doing things with server side code…

The express itself , I dont think can be extended as es6 class. What’s done instead is an App class which can be created as es6 module. This has an express instance, express still being imported as function which it is. So something like:

import express from 'express';

class App {
  constructor(config) {
    this.port = config.port;
    this.express = express();
  }
}

Now the trick is how to compile and run it as easy as normal npm start! Babel, babelify etc are the plugins which are used to convert es6 codes to normal javascript es5 to be run on engines, most of which are not supporting es6 out of the box. (including browsers, nodejs runtime etc). NodeJS still supports some es6 list of features with –harmony flag, but we all know thats not completely good way to go, if we are trying to have our code completely in es6.

We have to be using babel on the server side and babel-node is not good to be used in production. But yes, even though babel-node, which is the one which converts es6 to es5 and runs on node is not optimal, there are variants of the same which can be used. https://github.com/babel/example-node-server <- this gives the exact steps to be followed to have a babel based express server run in dev & production mode.

Finally got the simple stuff working. The result of this can be found here: https://github.com/Vivek-V-N/simple-tests/tree/master/express-es6-app

This might serve as a good learning code, if you are new to ES6 and express just like me!

Advertisements