Move to PUT, other changes
This commit is contained in:
parent
81e24ec50d
commit
e294d98fdf
3 changed files with 26 additions and 18 deletions
|
|
@ -11,7 +11,7 @@ export default (app: Application) => {
|
||||||
try {
|
try {
|
||||||
const urlObj = await Urls.findOne({
|
const urlObj = await Urls.findOne({
|
||||||
shortid: req.params.shortid
|
shortid: req.params.shortid
|
||||||
});
|
})
|
||||||
|
|
||||||
if (urlObj) {
|
if (urlObj) {
|
||||||
res.redirect(urlObj.url)
|
res.redirect(urlObj.url)
|
||||||
|
|
@ -19,28 +19,26 @@ export default (app: Application) => {
|
||||||
res.status(404).end()
|
res.status(404).end()
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error(ex);
|
console.error(ex)
|
||||||
res.status(500).send({
|
res.status(500).send({
|
||||||
error: ex
|
error: ex
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/add', async (req, res) => {
|
app.put('/', async (req, res) => {
|
||||||
if (req.query.url) {
|
if (req.body.url) {
|
||||||
/* Save DB room, try to find one first: */
|
|
||||||
const tmp = await Urls.findOne({
|
const tmp = await Urls.findOne({
|
||||||
url: String(req.query.url)
|
url: String(req.body.url)
|
||||||
})
|
})
|
||||||
|
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
res.status(200).send(tmp)
|
res.json(tmp)
|
||||||
} else {
|
} else {
|
||||||
const urlObj = new Urls()
|
const newUrl = new Urls()
|
||||||
urlObj.shortid = shortid.generate()
|
newUrl.url = String(req.body.url)
|
||||||
urlObj.url = String(req.query.url)
|
await newUrl.save()
|
||||||
await urlObj.save()
|
res.json(newUrl)
|
||||||
res.status(200).send(urlObj)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.status(422).end()
|
res.status(422).end()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
import mongoose, { Document, Schema, Types } from 'mongoose'
|
import mongoose, { Document, Schema, Types } from 'mongoose'
|
||||||
|
import shortid from 'shortid'
|
||||||
|
|
||||||
const UrlSchema: Schema = new Schema({
|
const UrlSchema: Schema = new Schema({
|
||||||
shortid: String,
|
shortid: {
|
||||||
|
type: String,
|
||||||
|
default: shortid.generate,
|
||||||
|
unique: true
|
||||||
|
},
|
||||||
url: String
|
url: String
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,19 @@ window.addEventListener('load', () => {
|
||||||
form.addEventListener('submit', async (evt) => {
|
form.addEventListener('submit', async (evt) => {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(`/add?url=${urlIn.value}`, {
|
const resp = await fetch('/', {
|
||||||
method: 'POST',
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
url: url
|
"url": String(urlIn.value)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
const json = await resp.json();
|
if (resp.status == 200) {
|
||||||
urlIn.value = `${location.origin}/${json.shortid}`;
|
const json = await resp.json();
|
||||||
|
urlIn.value = `${location.origin}/${json.shortid}`;
|
||||||
|
} else {
|
||||||
|
alert(`There was a server error: ${resp.statusText}`);
|
||||||
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
alert(`There was an error processing your request: ${ex}`);
|
alert(`There was an error processing your request: ${ex}`);
|
||||||
console.error(ex);
|
console.error(ex);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue