Categories
Fastify

Server-Side Development with Fastify — Rewrite URL and Loading Events

Spread the love

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Rewrite URL

We can rewrite URLs with the rewriteUrl option.

For example, we can write:

const fastify = require('fastify')({
  rewriteUrl (req) {
    return req.url === '/hi' ? '/hello' : req.url;
  }
})

fastify.get('/hello', (request, reply) => {
  reply.send('hello world')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then when we go to /hi , the /hello route is invoked.

Server Methods

fastify.server is the Node core server object returned by the Fastify factory function.

The after method lets us run code after current plugin and all the plugins registered within it have finished loading.

For example, we can write:

const fastify = require('fastify')()
fastify
  .register((instance, opts, done) => {
    console.log('Current plugin')
    done()
  })
  .after(err => {
    console.log('After current plugin')
  })
  .register((instance, opts, done) => {
    console.log('Next plugin')
    done()
  })
  .ready(err => {
    console.log('Everything has been loaded')
  })

fastify.get('/', (request, reply) => {
  reply.send('hello world')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then we see:

Current plugin
After current plugin
Next plugin
Everything has been loaded

logged in the console.

If after is called without a function, then it returns a promise.

For instance, we can write:

(async ()=>{
  const fastify = require('fastify')()
  fastify.get('/', (request, reply) => {
    reply.send('hello world')
  })

  await fastify.register(async (instance, opts) => {
    console.log('Current plugin')
  })

  await fastify.after()
  console.log('After current plugin')

  await fastify.register(async (instance, opts) => {
    console.log('Next plugin')
  })

  await fastify.ready()

  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
})()

We call fastify.register with an async function to load our plugins.

Then we call fastify.after with await to run code after the plugin is loaded.

We can call the fastify.ready to watch when tyhe server is ready to handle requests.

For example, we can write:

const fastify = require('fastify')()

fastify.get('/', (request, reply) => {
  reply.send('hello world')
})

fastify.ready(err => {
  if (err) throw err
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to add a callback to it.

We can also make it return a promise if we don’t pass in a callback:

const fastify = require('fastify')()

fastify.get('/', (request, reply) => {
  reply.send('hello world')
})

fastify.ready(err => {
  if (err) throw err
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
    await fastify.ready()
    console.log('successfully booted!')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Listen to Requests

We can listen for requests with the fastify.listen method.

For example, we write:

const fastify = require('fastify')()

fastify.get('/', (request, reply) => {
  reply.send('hello world')
})

fastify.ready(err => {
  if (err) throw err
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to start listening to requests with fastify.listen .

The first argument is the port to listen to and the 2nd argument is the IP address to listen to.

Conclusion

We can rewrite URLs and listen to various events with Fastify.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *