Adding Custom App Modifications

Documentation has moved.

The documentation for Flitter has moved. You probably want to be here: https://flitter.glmdev.tech/

From time to time, it may be useful for you (the developer) to add custom functionality to Flitter during initialization. This may be custom Express add-ons, tweaking settings of the underlying Express app, or any number of other things. As such, Flitter comes with a MiscUnit for these customizations. This unit in the file app/MiscUnit.js and follows the exact same structure as every other unit. It is loaded after the core Flitter units, but before any secondary or custom units. See libflitter/Unit for more information on how to add functionality to the unit.

Example: Adding the express-graphql Middleware

As an example, we'll configure the underlying Express app to use the express-graphql package on the route /graphql. To do this, modify the app/MiscUnit.js file:

const Unit = require('libflitter/Unit')

class MiscUnit extends Unit {
    
    constructor(){
        super()
        
        
    }
   
    go(app, next){
      	// Get the GraphQL package
        const graphqlHTTP = require('express-graphql')
        
        // Tell the underlying express middleware to use it
        app.use('/graphql', graphqlHTTP({
            schema: MyGraphQLSchema,
            graphiql: true
        }))
        
        next()
    }
    
    provides() {
        return {
            name: "misc"
        }
    }

}

module.exports = exports = MiscUnit

Here, we modify the go() method. This method is passed an instance of the underlying Express application (instance of express), which we can then use it as always.

No Comments
Back to top