Skip to content

Instantly share code, notes, and snippets.

@maximilian-krauss
Created October 13, 2020 05:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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