Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fastify = require('fastify').default // npm install fastify
let sessionId = 0
async function fireAndForget () {
const server = fastify()
.route({
url: '/login',
method: 'POST',
handler: async (request) => {
const { username } = request.body
return { token: `${username}-${++sessionId}` }
}
})
.route({
url: '/protected-route',
method: 'GET',
handler: async (request, reply) => {
const { authorization } = request.headers
if (!authorization || authorization.indexOf('foobaer') < 0) return reply.status(401).send({ error: 'No token provided!' })
return { usedToken: authorization }
}
})
await Promise.all([
server.listen(8080, '0.0.0.0'),
server.ready()
])
server.log.info('Ready to receive requests')
}
fireAndForget()
.then(() => console.log('done'))
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment