Fastify NodeJS server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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