0

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.

Moyo Falaye
  • 955
  • 1
  • 12
  • 24
  • I'm not clear on what the `db` object is doing. Aren't all arguments to `app.post` besides the first treated as callbacks? – sgress454 Jul 01 '14 at 22:41
  • Thank you for your interest. My app needs to connect to multiple databases at run time ( multi tenant app (connects to clients database after looking up who the client is from another database)). So the db object is the clients db connection. That is the only way i know to connect and pass client db connection during runtime. – Moyo Falaye Jul 01 '14 at 22:46
  • If `db` isn't a function, I still don't get what it's doing or where you're "passing" it to; the third argument to your `app.post` is a regular callback and can't use `db` at all. Is `db` a middleware function like what you described in your question, that handles the actual db connection? – sgress454 Jul 01 '14 at 23:31
  • @ScottGress Thank you. Please make a formal answer to remove the db object so i can give you credit for it. Thank you. Some times i Embarrass myself with the Obvious. – Moyo Falaye Jul 01 '14 at 23:46
  • You're welcome, but I was just trying to get some background--I don't think I answered your original question. Now that I know more, I can give it a shot. – sgress454 Jul 02 '14 at 17:23

1 Answers1

1

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.

sgress454
  • 24,870
  • 4
  • 74
  • 92