Moyo Falaye
Moyo Falaye

Reputation: 995

sailsjs + ext-direct (sencha extjs)

I was wondering if anyone has used ext-direct with sailsjs. If you have or anyone with the know how, please can you direct me.

Thank you.

Here are some examples

In regular nodejs/express app, I would normaly do this in my app.js file

app.get(ExtDirectConfig.apiPath, function (request, response) {
    try {
        var api = extdirect.getAPI(ExtDirectConfig);
        response.writeHead(200, {'Content-Type': 'application/json'});
        response.end(api);
    } catch (e) {
        console.log(e);
    }
});

// Ignoring any GET requests on class path
app.get(ExtDirectConfig.classPath, function (request, response) {
    response.writeHead(200, {'Content-Type': 'application/json'});
    response.end(JSON.stringify({success: false, msg: 'Unsupported method. Use POST      instead.'}));
});

// POST Request process route and calls class
app.post(ExtDirectConfig.classPath, db, function (request, response) {
    extdirect.processRoute(request, response, ExtDirectConfig);
});

How would i do this in sails.js

Edit: Thank you @Scott Gress. After looking over my code, there was no need to pass the db object (yes it is a middleware) as it has already attached itself to the request object. Thank you.

Thank you.

Upvotes: 0

Views: 371

Answers (1)

sgress454
sgress454

Reputation: 24948

The simplest way to do this would be using the customMiddleware config option of Sails. This option allows you to supply a function that will receive the underlying Express app as its sole argument, to which you can add your own routes or middleware. Find or create your config/express.js file and put in something like this:

// First, do all requires / setup necessary so that 
// `extdirect` and `ExtDirectConfig` exist, then:
module.exports.express = {

    customMiddleware: function(app) {

        app.get(ExtDirectConfig.apiPath, function (request, response) {
            try {
                var api = extdirect.getAPI(ExtDirectConfig);
                response.writeHead(200, {'Content-Type': 'application/json'});
                response.end(api);
            } catch (e) {
                console.log(e);
            }
        });

        // Ignoring any GET requests on class path
        app.get(ExtDirectConfig.classPath, function (request, response) {
            response.writeHead(200, {'Content-Type': 'application/json'});
            response.end(JSON.stringify({success: false, msg: 'Unsupported method. Use POST      instead.'}));
        });

        // POST Request process route and calls class
        app.post(ExtDirectConfig.classPath, db, function (request, response) {
            extdirect.processRoute(request, response, ExtDirectConfig);
        });

    }
}

A more involved, but ultimately more re-usable strategy would be to create a custom "hook", or Sails plugin, for ExtDirect. Documentation for custom hooks is in development here.

Upvotes: 1

Related Questions