Migrate your Express js Project to Sails js Using Sails Hook

Author - Pratik Gadoya

Nowadays Node js Express.js is so much trending but as early users, we were not having any MVC framework like Sails.js. So Sails.js concept is so much similar to Ruby on Rails or I can say so much similar to Laravel (Trending php framework) also. So for the developer using Express.js there must be some easy way to use Sails.js. Keeping that in my mind I am inspired to write “Migrate your Express.js project to sails.js”.

Sails.js is basically built on top of different kind of hooks. By creating new hook we can easily extend sails.js core functionality. But apart from sails.js official documentation we don’t get more things about hook. We can write many kind of different hooks as per our requirement.

For this blog we will create hook for overriding sails.js http hook so that for our particular routes, we can serve request directly from our old Express project. Vise-versa it allows us to write new functionality and new routes in sails.js and hence enables us to use full MVC architecture of sails.js. So let’s dive in to code directly.

Required changes for Express.js app.js file

So currently to run express.js project we would be running node app.js

For migration obviously, we won’t be able to run two servers in parallel. So I am adding demo app.js file here so that we can understand how to export our express object which we are using in current Express.js project. This just includes “hello-word” for understanding. We just need to try and keep our Express.js project as it is so that we can migrate easily.

const express = require("express");

module.exports = function(config) {
  var app = express();
  app.get("/hello-world", (req, res) => res.send("Hello World!"));
  return app;
};

Ok that’s the only thing we need to change in our app.js file.

Writing Sails hook

I am hoping the reader has some basic knowledge of sails.js if not please refer here.

We need to create a new Sails.js project, please follow steps here.

Let’s keep both the project side by side as given in the image.

And then let’s start editing file
`sails.js/config/http.js`

Migrate Expressjs to Sailsjs

Final step to “Migrate Express.js to sails.js”

http.js file

const express = require("express");
const appJs = require("../../expressjs/app");

module.exports.http = {

  middleware: {
    order: [
      "expressHook"
    ],

    expressHook: (function() {
      const appWrapper = express();
      const appJsMigrateInstance = appJs();
      appWrapper.use(appJsMigrateInstance);
      return appWrapper;
    })()
  }
};

Basically here we are extending express HTTP middleware and asking it to use our express object exported in app.js

The only key part is appWrapper.use(appJsMigrateInstance); this asks our sails.js project to use all the defined routes in the expressjs/app.js file.

With this code priority of the routes will be given to the expressjs/app.js file. So if any route not found in our app.js then n then that route will be served by our sails.js project.

All the cors, port and session settings will be applied to the sails.js. So we need to take care of those as per our project requirement.

Feel free to comment below if you have any doubt/questions.

And finally, as always GitHub code is ready for you. Happy sailsjsing ?.

Git for migrate-expressjs-to-sailsjs

Don’t miss the next post!

Loading

Related Posts