Init routes
This commit is contained in:
parent
2d7e6b57f6
commit
ea435a210b
1 changed files with 31 additions and 0 deletions
31
src/routes.ts
Normal file
31
src/routes.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Application } from 'express'
|
||||
import Urls from './schema'
|
||||
|
||||
export default (app: Application) => {
|
||||
app.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const urlObj = await Urls.findById(req.params.id);
|
||||
if (urlObj) {
|
||||
res.redirect(urlObj.url)
|
||||
} else {
|
||||
res.status(404).end()
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
res.status(500).send({
|
||||
error: ex
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/add', async (req, res) => {
|
||||
if (req.body.url) {
|
||||
const urlObj = new Urls()
|
||||
urlObj.url = req.body.url
|
||||
await urlObj.save()
|
||||
res.status(200).send(urlObj)
|
||||
} else {
|
||||
res.status(422).end()
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue