-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
35 lines (29 loc) · 811 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import express from 'express';
import { nanoid } from 'nanoid';
const app = express();
const port = process.env.PORT || 3000;
const urlDatabase = {};
app.use(express.static('public'));
app.use(express.json());
app.post('/shorten', (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}
const shortId = nanoid(7);
urlDatabase[shortId] = url;
const shortUrl = `${req.headers.host}/${shortId}`;
res.json({ shortUrl });
});
app.get('/:id', (req, res) => {
const { id } = req.params;
const originalUrl = urlDatabase[id];
if (originalUrl) {
res.redirect(originalUrl);
} else {
res.status(404).send('URL not found');
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});