mirror of
https://github.com/FranP-code/shortly.git
synced 2025-10-13 00:43:28 +00:00
fastify initial commit
This commit is contained in:
19
src/index.js
Normal file
19
src/index.js
Normal 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
16
src/routes/add-url.js
Normal 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
5
src/routes/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
module.exports = async function (fastify, options) {
|
||||
fastify.get('/', async (request, reply) => {
|
||||
return { test: 'testValue' }
|
||||
})
|
||||
}
|
||||
6
src/routes/url-shortener.js
Normal file
6
src/routes/url-shortener.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = async function (fastify, options) {
|
||||
fastify.get('/:id', async (request, reply) => {
|
||||
const { id } = request.params
|
||||
return { test: id }
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user