Express
Pikku can be added to an express server via middleware, or you can just use the PikkuExpressServer if everything is using pikku.
Live Example
Using Express Middleware
Pikku can be / is best used within express as a middleware function.
Express middleware
import { pikkuExpressMiddleware } from '@pikku/express-middleware'
import { createSingletonServices, createWireServices } from 'path/to/pikku-bootstrap.ts'
// The express server setup goes here...
const singletonServices = await createSingletonServices()
app.use(pikkuExpressMiddleware(
singletonServices,
createWireServices,
{
respondWith404: false,
logRoutes: true,
loadSchemas: true
}
))
Using PikkuExpressServer
note
The setup process for Express, uWS, and Fastify servers are identical, except for using different constructors.
PikkuExpressServer is a quick way to get an express server started with pikku if you don't need to put in any custom configuration.
Express start
loading...
This script does the following:
- Imports necessary modules from
@pikku/expressand your project's configuration and services. - Defines an async function
runServerthat:- Loads the Pikku configuration
- Creates singleton services
- Initializes a new
PikkuExpressServerinstance - Enables graceful shutdown on SIGINT
- Initializes and starts the server
- Handles any errors by logging them and exiting the process
- Calls the
runServerfunction to start the server
By following this setup, you can easily integrate Pikku with a Express server, benefiting from both Pikku's features and Express's ecosystem.