Tuesday 6 June 2017

Unable to create a versioned API in SailJS

First things first, version of various dependencies
Ubuntu - 15.04
NodeJS - 6.10.3
NPM - 3.10.10
Sails - 0.12.13

Second, here's what I did:
1. Installed Sails globally
2. In the directory /var/www, ran the command sails new app
3. Created a file UserController.js in api/controllers/v1
4. Created a file User.js in api/models

Code for UserController.js

module.exports = {

  findOne: function(req, res) {

    return res.send("Hello World!!! User -> findOne");
  },

  login: function(req, res) {

    return res.send("Hello World!!! User -> login");
  }
}

Code for User.js

module.exports = {}

Now, when I start my server using sails lift, here's what happens:


I know I'm going to sound silly, but I thought it is probably because I didn't use the Generator feature of Sails. So, here's what I did next: sails generate api v1/Product. And the file structure after this command is like this:

api  
|-- controllers  
    |-- v1  
        |-- UserController.js
    |-- V1  
        |-- ProductController.js
|-- models  
    |-- User.js  
    |-- V1  
        |-- Product.js  

I wrote similar code in ProductController.js as I did in UserController.js and I expected that now the Product API should work but the result was same as in case of /v1/user/1.


As per the SailJS Blueprint API, this should have worked. So, can anyone explain why this is happening and how can I make /v1/user/1 and /v1/user/login both work as expected.



via Rohit Aggarwal

No comments:

Post a Comment