-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (31 loc) · 951 Bytes
/
index.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
36
37
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const port = 3000;
// Connect to your SQLite database
let db = new sqlite3.Database('./db.sqlite', sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the SQLite database.');
});
// Capture all incoming URLs
app.get('*', (req, res) => {
const url = req.originalUrl;
// Query the database for the incoming URL
db.get(`SELECT redirectUrl FROM urls WHERE incomingUrl = ?`, [url], (err, row) => {
if (err) {
return console.error(err.message);
}
// If a match is found, redirect to the stored URL
if (row) {
res.redirect(row.redirectUrl);
} else {
// If no match is found, send a 404 response
res.status(404).send('URL not found');
}
});
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});