fastify initial commit

This commit is contained in:
2023-01-02 18:12:02 -03:00
commit a897ec5f95
10 changed files with 131 additions and 0 deletions

19
src/index.js Normal file
View File

@@ -0,0 +1,19 @@
const fastify = require('fastify')({ logger: true })
const addUrl = require('./routes/add-url')
const index = require('./routes/index')
const urlShortener = require('./routes/url-shortener')
fastify.register(index)
fastify.register(urlShortener)
fastify.register(addUrl)
const start = async () => {
try {
await fastify.listen({ port: process.env.PORT || 3000, host: '0.0.0.0' })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()

16
src/routes/add-url.js Normal file
View File

@@ -0,0 +1,16 @@
module.exports = async function (fastify, options) {
fastify.register(require('@fastify/jwt'), {
secret: 'supersecret123'
})
fastify.post('/add', (req, reply) => {
// some code
const token = fastify.jwt.sign({ abc: 123 })
console.log(token)
reply.send({ token })
})
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err
})
}

5
src/routes/index.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = async function (fastify, options) {
fastify.get('/', async (request, reply) => {
return { test: 'testValue' }
})
}

View File

@@ -0,0 +1,6 @@
module.exports = async function (fastify, options) {
fastify.get('/:id', async (request, reply) => {
const { id } = request.params
return { test: id }
})
}