1. #1
    Sencha User
    Join Date
    Nov 2009
    Location
    Aveiro, Portugal
    Posts
    8
    Vote Rating
    0
    jgrocha is on a distinguished road

      0  

    Default Running Ext.Direct Backend in Node.js using Passenger

    Running Ext.Direct Backend in Node.js using Passenger


    Hi,

    I've followed Juris's blog entry Writing an Ext.Direct Backend in Node.js and I wrote a small application (https://github.com/jgrocha/extdirect.examples). I've added some logic for user authentication, PostgreSQL and OpenLayers + GeoEXT2.

    Everything runs fine if I start the application from the command line:
    Code:
    nodemon server.js
    and the application is available from: http://localhost:3000/

    I would like to deploy it to a server already running Apache. I've installed Fusion Passenger, and created the VirtualHost entry, that reads:

    Code:
    <VirtualHost *:80>
            ServerName development.localhost.lan
    #       DocumentRoot /home/jgr/git/extdirect.examples/node-server/extdirect-pg/public
            DocumentRoot /home/jgr/git/extdirect.examples/client/DemoExtJs
            PassengerAppRoot /home/jgr/git/extdirect.examples/node-server/extdirect-pg
    </VirtualHost>
    I even separated the DocumentRoot and the AppRoot (because public was a link to the client application).

    When I try to run the application from Apache+Passenger, it starts the application, but immediately fails when trying to fetch:

    GET http://development.localhost.lan/dir...=1403100801391 404 (Not Found)


    Passenger starts the application. Passenger should know which requests are to the client and which request are to the server, right? So, this path (/directapi) should be handled by express in server.js, I think.

    Code:
    //GET method returns API
    app.get(ExtDirectConfig.apiPath, function(request, response) {
    (...)
    My server-config.json is configured like:

    Code:
    {
        "ServerConfig": {
            "port": 3000,
            "logger": "dev",
            "enableUpload": true,
            "enableCompression": true,
            "webRoot": "/public",
            "fileUploadFolder": "./uploads",
            "enableSessions": true,
            "sessionSecret": "fe80::227:10ff:fe7e:fcb4",
            "enableCORS": true,
            "AccessControlAllowOrigin": "*",
            "AccessControlAllowMethods": "GET,PUT,POST,DELETE,OPTIONS",
            "AccessControlAllowHeaders": "Origin, X-Requested-With, Content-Type, Accept",
            "AccessControlAllowCredentials": true,
            "AccessControlMaxAge": 3600
        },
        "ExtDirectConfig": {
            "namespace": "ExtRemote",
            "apiName": "REMOTING_API",
            "apiPath": "/directapi",
            "classPath": "/direct",
            "classPrefix": "DX",
            "server": "development.localhost.lan",
            "port": "3000",
            "protocol": "http",
            "relativeUrl": true,      
            "appendRequestResponseObjects": true
        }
    }
    The DirectAPI.js loads the API with
    Code:
    Loader.loadScriptFile('directapi', Ext.emptyFn, Ext.emptyFn, null, true);
    Any help for experienced users is appreciated, since I'm not able to identify if this is a Passenger configuration issue or a node/ext.direct issue.

    Thanks in advance.

  2. #2
    Sencha User
    Join Date
    Nov 2009
    Location
    Aveiro, Portugal
    Posts
    8
    Vote Rating
    0
    jgrocha is on a distinguished road

      0  

    Default extdirect.processRoute

    extdirect.processRoute


    Hi,

    It seems that the problem is related to the following code in server.js:
    Code:
    // POST request process route and calls class
    app.post(ExtDirectConfig.classPath, function(request, response) {
    	extdirect.processRoute(request, response, ExtDirectConfig);
    });
    Posts to the API are not properly routed:
    Code:
    POST http://development.localhost.lan/direct 500 (Internal Server Error)
    The error is:

    Code:
    Error: Cannot find module '/usr/share/passenger/helper-scripts/direct/DXTree'    at Function.Module._resolveFilename (module.js:338:15)    at Function.Module._load (module.js:280:25)    at Module.require (module.js:364:17)    at require (module.js:380:17)    at Object.route.processRoute (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/node_modules/extdirect/lib/router.js:94:17)    at app.configure.app.use.express.errorHandler.dumpExceptions (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/server.js:146:12)    at callbacks (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/node_modules/express/lib/router/index.js:161:37)    at param (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/node_modules/express/lib/router/index.js:135:11)    at pass (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/node_modules/express/lib/router/index.js:142:5)    at Router._dispatch (/home/jgr/git/extdirect.examples/node-server/extdirect-pg/node_modules/express/lib/router/index.js:170:5)
    I took a look at node_modules/extdirect/lib/router.js and I found onw thing that my cause problems.

    Code:
    var vPath = require('path');
    
    
    var route = {
        processRoute : function(req, resp, config){
            var root = vPath.dirname(process.mainModule.filename),
    This process.mainModule.filename returns different paths, if I run the application directly with node or with passenger.

    With node: /home/jgr/git/extdirect.examples/node-server/extdirect-pg
    With Passenger: /usr/share/passenger/helper-scripts

    Changing this path to the correct one (static path hard coded) fixed this problem.

    To fix the problem of loading /directapi, I created a javascript file on the client side (iƩ public) with the API within.

    So, instead of:
    Loader.loadScriptFile('/directapi', Ext.emptyFn, Ext.emptyFn, null, true);
    I put:
    Loader.loadScriptFile('/ppgis.js', Ext.emptyFn, Ext.emptyFn, null, true);

    This is too much fixing. Giving up on Passenger for now...

  3. #3
    Sencha User
    Join Date
    Nov 2009
    Location
    Aveiro, Portugal
    Posts
    8
    Vote Rating
    0
    jgrocha is on a distinguished road

      0  

    Default Problem solved

    Problem solved


    Hi,

    I took a look at Juris's code and I was able to fix the routing either when the application runs from node or from Apache+Passenger. Now /directapi and /direct calls are resolved.
    I've made a pull request with my two cents to solve the problem.

    Regards!