sails.jsmiddlewarelti

Integration of Ltijs into existing sails app


I want to use the Ltijs library (https://cvmcosta.me/ltijs) in our Sails application. The way to deploay Ltijs as part of another express server is this (from Ltijs documentation):

    const app = express()
    lti.setup('EXAMPLEKEY', { url: 'mongodb://localhost/database' })
    
    // Start LTI provider in serverless mode
    await lti.deploy({ serverless: true })
    
    // Mount Ltijs express app into preexisting express app with /lti prefix
    app.use('/lti', lti.app)

The way to put middleware into Sails is something like that (without app.use(...)!):

// config/http.js

const lti = require('ltijs').Provider;
lti.setup(
        '8swieleivBef',
        {url: 'mongodb://localhost:27017/mysailsapp'},
);

lti.deploy({serverless: true});

module.exports.http = {

  middleware: {

    order: [
      'cookieParser',
      'session',
      'bodyParser',
      'ltiExpressAdapter', //<-------- my middleware adapter -----------------
      'compress',
      'poweredBy',
      'router',
      'www',
      'favicon',
    ],

    ltiExpressAdapter: lti.app, //<-------- my middleware adapter -----------------
    
    .
    .
    .

The latter works, but it works to "good", because now every request is caught by Ltijs and the application doesn't work anymore. My Question is, how do I bring the path '/lti' from app.use('/lti', lti.app) into the sails game?

I tried lots of things like this that didn't work:

ltiExpressAdapter: (function () {
  return async function (req, res, next) {
    if (req.path.match(/^\/lti.*$/)) {
      return lti.app;
    }
    return next();
  };
})(),

Thanks in advance for help!


Solution

  • Seems I found a solution using app.use(...):

    // config/http.js
    
    const express = require('express');   //<------ NEW -----
    const app = express();                //<------ NEW -----
    
    const lti = require('ltijs').Provider;
    lti.setup(
            '8swieleivBef',
            {url: 'mongodb://localhost:27017/ltijsdb'},  //<------ (NEW) -----
    );
    
    lti.deploy({serverless: true});
    
    module.exports.http = {
    
      middleware: {
    
        order: [
          'cookieParser',
          'session',
          'bodyParser',
          'ltiExpressAdapter', //<-------- my middleware adapter -----------------
          'compress',
          'poweredBy',
          'router',
          'www',
          'favicon',
        ],
    
        ltiExpressAdapter: app.use('/lti', lti.app), //<------ NEW -----
        
        .
        .
        .
    

    Now I get the expexted error message from Ltijs only when I call the /lti path (http://localhost:1337/lti) and the rest of the application runs like before.

    Now I hopefully can go on setting up Ltijs and try to connect from a test consumer.

    (I also changed the Ltijs DB so that it isn't mixed up with my App's DB.)