Cvmcosta / ltijs

Turn your application into a fully integratable LTI 1.3 tool provider.
https://cvmcosta.github.io/ltijs/
Apache License 2.0
300 stars 67 forks source link

How to serve an Angular build #37

Closed ahelord closed 4 years ago

ahelord commented 4 years ago

I want to redirect to the Angular build dist get this log and it sends me to invalidToken

image

image

Cvmcosta commented 4 years ago

In order to serve static files (like your js files generated by angular), just like express you need to declare a static file path, in Ltijs you can do this by using the staticPath parameter in the constructor options object, example:

const lti = new LTI('EXAMPLEKEY', 
            { url: 'mongodb://localhost/database' }, 
            { appUrl: '/', staticPath: path.join(__dirname, 'public') })

further information: Serving static files in Express

ahelord commented 4 years ago

@Cvmcosta thank you very much.


const express = require('express')
// Require Provider
const LTI = require('ltijs').Provider
// Loading environment variables
const APP_FOLDER = 'public/dist/app/';

//require('dotenv').config()

// Creating a provider instance
const lti = new LTI('LTI_KEY',
  // Setting up database configurations
  { url: 'mongodb://localhost/test' },
  { appUrl: '/', loginUrl: '/login', logger: false,
    staticPath:APP_FOLDER})

  let setup = async () => {
    // Deploy and open connection to the database
    await lti.deploy()

    // Register platform
    const plat = await lti.registerPlatform({
      url: 'http://localhost:8081',
      name: 'Local Moodle 2',
      clientId: 'rbJKqLxYKEZRnGt',
      authenticationEndpoint: 'http://localhost:8081' + '/mod/lti/auth.php',
      accesstokenEndpoint: 'http://localhost:8081' + '/mod/lti/token.php',
      authConfig: { method: 'JWK_SET', key: 'http://localhost:8081' + '/mod/lti/certs.php' }
    })
    lti.onConnect((connection, request, response) => {
      // Call redirect function
      lti.redirect(response, '/main?locals='+response.locals.context.custom.apiKey)
    })

    // Set main endpoint route
    lti.app.get('/main', (req, res) => {
      // Id token
      console.log(res.locals)
      //res.send('It\'s alive!')
      console.log(__dirname)
      res.status(200).sendFile(`/`, {root: APP_FOLDER});

    })
}

  setup()