Adds Express to your application.
- Setup Guide for adding middleware and routes.
- Common "Gotchas" encountered with Express middleware.
gasket create <app-name> --plugins @gasket/plugin-express
npm i @gasket/plugin-express
Modify plugins
section of your gasket.config.js
:
module.exports = {
plugins: {
add: [
+ '@gasket/plugin-express'
]
}
}
All the configurations for the plugin are added under express
in the config:
compression
: true by default. Can be set to false if applying compression differently.excludedRoutesRegex
: Routes to be excluded based on a regex
module.exports = {
plugins: {
add: ['@gasket/express']
},
express: {
compression: false,
excludedRoutesRegex: /^(?!\/_next\/)/
}
}
Executed when the express
server has been created, it will apply all returned
functions as middleware.
module.exports = {
hooks: {
/**
* Add Express middleware
*
* @param {Gasket} gasket The Gasket API
* @param {Express} app - Express app instance
* @returns {function|function[]} middleware(s)
*/
middleware: function (gasket, app) {
return require('x-xss-protection')();
}
}
}
You may also return an Array
to inject more than one middleware.
Executed after the middleware
event for when you need full control over
the express
instance.
module.exports = {
hooks: {
/**
* Update Express app instance
*
* @param {Gasket} gasket The Gasket API
* @param {Express} express Express app instance
* @returns {function|function[]} middleware(s)
*/
express: async function (gasket, express) {
}
}
}
Executed after the express
event. All middleware functions returned from this
hook will be applied to Express.
module.exports = {
hooks: {
/**
* Add Express error middlewares
*
* @param {Gasket} gasket The Gasket API
* @returns {function|function[]} error middleware(s)
*/
errorMiddleware: function (gasket) {
}
}
}
This plugins hooks the createServers lifecycles from @gasket/plugin-https.