Skip to content

Instantly share code, notes, and snippets.

@sekoyo
Last active December 24, 2021 13:07
Show Gist options
  • Save sekoyo/8a56ae98481e407c02f64e1f42abb5b0 to your computer and use it in GitHub Desktop.
Save sekoyo/8a56ae98481e407c02f64e1f42abb5b0 to your computer and use it in GitHub Desktop.
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