Added leveldb

This commit is contained in:
Nicolai Ort 2021-08-12 19:22:14 +02:00
parent c6b02b7eb4
commit 97a86db5e3
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
2 changed files with 26 additions and 13 deletions

View File

@ -4,7 +4,6 @@
"main": "index.js",
"license": "MIT",
"private": false,
"type": "module",
"scripts": {
"dev": "nodemon src/server.js",
"start": "node src/server.js"
@ -12,6 +11,7 @@
"dependencies": {
"fastify": "^3.20.1",
"knex": "^0.21.21",
"level": "^7.0.0",
"sqlite3": "^5.0.2"
},
"devDependencies": {

View File

@ -1,32 +1,45 @@
const fastify = require('fastify')({ logger: true })
const level = require('level')
const db = level('./db', { valueEncoding: 'json' })
// Declare a route
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
return { hello: 'world' }
})
fastify.get('/a/:id', async (req, res)=>{
//Automagic Amazn redirects on /a/
fastify.get('/a/:id', async (req, res) => {
res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
})
fastify.get('/:id', async (req, res)=>{
res.statusCode = 301;
return req.params.id;
//Automagic Youtube redirects on /yt/
fastify.get('/yt/:id', async (req, res) => {
res.redirect(302, `https://youtu.be/${req.params.id}`)
})
fastify.get('/:id', async (req, res) => {
try {
const target = await db.get(req.params.id);
res.redirect(302, target);
} catch (error) {
res.statusCode = 404;
return 404;
}
})
fastify.post('/new', async (req, res) =>{
fastify.post('/new', async (req, res) => {
console.log(req.body);
return "";
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()