Update
- Make all errors include stack - Make all responses JSON format
This commit is contained in:
parent
43c0becdb9
commit
9bfe9206b4
20
index.js
20
index.js
|
@ -12,7 +12,7 @@ app.use(express.json());
|
|||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
res.sendStatus(404);
|
||||
res.status(404).json({ error: 'Not found' });
|
||||
});
|
||||
|
||||
// Add your routes and middleware here
|
||||
|
@ -21,7 +21,7 @@ app.get('/:shortUrl', (req, res) => { // Shortened URL
|
|||
db.get('SELECT url FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
if (row) {
|
||||
res.redirect(row.url);
|
||||
|
@ -36,7 +36,7 @@ app.get('/:shortUrl', (req, res) => { // Shortened URL
|
|||
}
|
||||
}
|
||||
else {
|
||||
res.sendStatus(404);
|
||||
res.status(404).json({ error: 'Short URL not found' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -53,13 +53,13 @@ app.get('/stats/:shortUrl', (req, res) => { // Stats
|
|||
db.all('SELECT * FROM urls', (err, rows) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
rows.forEach((row, i) => {
|
||||
db.all('SELECT * FROM refs WHERE shortUrl = ?', [row.shortUrl], (err, refs) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
rows[i].refs = refs;
|
||||
if (i === rows.length - 1) {
|
||||
|
@ -73,19 +73,19 @@ app.get('/stats/:shortUrl', (req, res) => { // Stats
|
|||
db.get('SELECT * FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
if (row) {
|
||||
db.all('SELECT * FROM refs WHERE shortUrl = ?', [shortUrl], (err, rows) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
res.json({ url: row.url, shortUrl: row.shortUrl, visits: row.visits, refs: rows });
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.send("Code not found").status(404);
|
||||
res.status(404).json({ error: 'Short URL not found' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
|
|||
db.get('SELECT shortUrl FROM urls WHERE shortUrl = ?', [shortUrl], (err, row) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
if (row) {
|
||||
return res.status(400).json({ error: 'Short URL already in use' });
|
||||
|
@ -114,7 +114,7 @@ app.post('/shorten', (req, res) => { // Shorten URL
|
|||
db.run('INSERT INTO urls (shortUrl, url) VALUES (?, ?)', [shortUrl, req.body.url], (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.sendStatus(500);
|
||||
return res.status(500).json({ error: 'Internal server error', fullError: err.stack });
|
||||
}
|
||||
res.json({ shortUrl });
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue