Skip to content

Instantly share code, notes, and snippets.

@IskenHuang
Last active December 24, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IskenHuang/b5bb5bc9b56a15452d6e to your computer and use it in GitHub Desktop.
Save IskenHuang/b5bb5bc9b56a15452d6e to your computer and use it in GitHub Desktop.
sails controller differentiate api and view
/**
* Controllers
*
* By default, Sails controllers automatically bind routes for each of their functions.
* Additionally, each controller will automatically bind routes for a CRUD API
* controlling the model which matches its name, if one exists.
*
* NOTE: These settings are for the global configuration of controllers.
* You may also override these settings on a per-controller basis
* by modifying the 'blueprints' object in your controllers
*
* For more information on controller configuration and blueprints, check out:
* http://sailsjs.org/#documentation
*/
module.exports.controllers = {
blueprints: {
// Optional mount path prefix for blueprints
// (the automatically bound routes in your controllers)
// e.g. '/api/v2'
prefix: '/api',
// Whether routes are automatically generated for every action in your controllers
// (also maps `index` to /:controller)
// '/:controller', '/:controller/index', and '/:controller/:action'
actions: true,
// ** NOTE **
// These CRUD shortcuts exist for your convenience during development,
// but you'll want to disable them in production.
// '/:controller/find/:id?'
// '/:controller/create'
// '/:controller/update/:id'
// '/:controller/destroy/:id'
shortcuts: true,
// Automatic REST blueprints enabled?
// e.g.
// 'get /:controller/:id?'
// 'post /:controller'
// 'put /:controller/:id'
// 'delete /:controller/:id'
rest: true,
// If a blueprint route catches a request,
// only match :id param if it's an integer
//
// e.g. only trigger route handler if requests look like:
// get /user/8
// instead of:
// get /user/a8j4g9jsd9ga4ghjasdha
expectIntegerId: false
}
};
/**
* Routes
*
* Sails uses a number of different strategies to route requests.
* Here they are top-to-bottom, in order of precedence.
*
* For more information on routes, check out:
* http://sailsjs.org/#documentation
*/
module.exports.routes = {
'/': 'ViewController.index',
'/:page': {
controller: 'ViewController',
action: 'page'
}
};
/api
    /adapters
        ...
    /controllers
        ViewController.js
        ...
    /models
        ...
    /services
        ...
/assets
    /bower_components
        ...
    /images
        ...
    /scripts
        ...
    /styles
        ...
    /fonts
        ...
    favicon.ico
    robots.txt
/config
    /locales
        ...
    404.js
    500.js
    adapters.js
    bootstrap.js
    controller.js
    cors.js
    csrf.js
    i18n.js
    local.js
    log.js
    policies.js
    routes.js
    session.js
    sockets.js
    views.js
/node_modules
    ...
/views
    /home
        index.ejs
        ...
.bowerrc
.gitignore
app.js
bower.json
Gruntfile.js
package.json
README.md
/**
* ViewController
*
* @module :: Controller
* @description :: Contains logic for handling requests.
*/
module.exports = {
index: function(req, res) {
console.log('viewController index begin');
return res.view('home/index.ejs', {
// layout default in the same folder
// layout: 'layout',
});
},
/**
* redirect to right page
* i.e http://localhost:3000/user
* ^^^^^^
* page = user
* controller will redirect to ViewController/user
*
* @param {String} page redirect to page
*/
page: function(req, res) {
var page = req.param('page');
return sails.controllers.view[page](req, res);
},
user: function(req, res) {
return res.view('user/index.ejs');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment