Exceptions
Exceptions

Reputation: 1204

SailsJS. Custom /api URL for JSON

Currently, I am using localhost:1337/users to render a view with a table to show users's data.

How can I get a custom URL for example localhost:1337/api/users to output JSON data.

SailsJS does blueprint automatically but after showing the view thru the controller, the JSON output gets overridden.

Upvotes: 0

Views: 388

Answers (1)

Bonanza
Bonanza

Reputation: 1621

You can use Blueprint target syntax. You need to add in config/routes.js

'GET /api/users': {model:'users', blueprint: 'find'}

or something similar.

If you want to return JSON from controller you should create second controller method calling same model method.

UserController.js

getUsersJson: function(req, res){
    Users.get(params,function(errors,results){
        res.json(results);
    });
}

config/routes.js

'GET /api/users': 'UserController.getUsersJson'

Upvotes: 2

Related Questions