Fastify/4/Remove App Use

/icons/calendar.svg

Last update

Aug 26, 2024

This codemod removes app.use() and the use of middleware is no longer supported.

🚦 Impact Level: Minimal

What Changed

With v4 of Fastify, app.use() has been removed and the use of middleware is no longer supported.

If you need to use middleware, use @fastify/middie or @fastify/express, which will continue to be maintained. However, it is strongly recommended that you migrate to Fastify's hooks.

Before

const fastify = require('fastify')();
fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});
fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});
fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});

After

const fastify = require('fastify')();
const middie = require('@fastify/middie');
fastify.register(middie);
fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});
fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});
fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now