sailsjs + ext-direct (sencha extjs)

百般思念 提交于 2019-12-25 02:51:40

问题


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.


回答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.



来源:https://stackoverflow.com/questions/24519858/sailsjs-ext-direct-sencha-extjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!