Skip to content

Instantly share code, notes, and snippets.

@DominicTobias
Last active December 24, 2021 13:07
Embed
What would you like to do?
Fastify NodeJS server example
import Fastify from 'fastify'
import middie from 'middie'
import cors from 'cors'
import frameguard from 'frameguard'
import xXssProtection from 'x-xss-protection'
const fastify = Fastify({
logger: true,
})
fastify.get('/', async (req, reply) => {
reply.type('application/json').code(200)
return { pong: Date.now() }
})
async function start() {
try {
// Register middleware handler.
await fastify.register(middie)
// Add middleware.
fastify.use(cors())
fastify.use(frameguard())
fastify.use(xXssProtection())
// Start server.
const port = process.env.PORT || 8396
const address = await fastify.listen(port, '0.0.0.0')
console.log(`⚡️ Listening on ${address}`)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment