Moving from SailsJs to ExpressJs
SailsJs is great for beginners but like all frameworks it has its limitations and defects. Its development activity has slowed down considerably and it is stuck with Express 3.x which is not even supported by Express js itself. So I decided to port my old sailsjs code to pure expressjs.
First I needed to replicate the folder structure similar to saisjs. So that I could port my code without much of hassle. In my sails app I did not use my views since I kept the UI seperate and used sailsjs more like a rest endpoint. And as for ORM I thought of using waterline for now and later port it to mongoose.
the folder structure of the app is as follows
App
-api
-controllers
-models
-policies
-servies
-config
-bootstrap.js
-connection.js
-cors.js
-error.js
-globals.js
-middleware.js
-policies.js
-routes.js
-app.js
-package.json
First I installed the base packages I needed and here is my packages.json
To get the app running we need app.js here is my complete app.js
Here I have required modules express, connect and body-parser for creating express app. cors module to enable cors support and command-line-args arguments to get command line parameters although here I have accepted only one argument -p for port. the module ./config/policies contains list of policies to be executed before the controllers and ./config/connection is used to setup waterline connection. In my bootstrap.js I have searched for file ending with name controllers.js in controllers directory and I have set them as global variables in express app so that those controllers will be hit when using appropriate route using map. the routes.js is used to configure routes and file name with suffix controllers.js will be added to route by default else you need to do it manually.
here is the full code.