Skip to content

Commit

Permalink
Add URL formatting logic in server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlee-06 committed Feb 6, 2025
1 parent a90e4ea commit bd55bfd
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ const disallowedIds = ['public', 'admin', 'docs', 'database.js', 'server.js', 'p
const disallowedSymbols = ['/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.', '\''];

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static(path.join(__dirname)));


app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/public/'));
const requestedUrl = req.query.u;

if (!requestedUrl) {
res.sendFile(path.join(__dirname, '/public/'));
}

else {
res.redirect(formatUrl(requestedUrl));
}

});

// 接受 POST 請求的伺服器
Expand Down Expand Up @@ -53,6 +62,7 @@ app.post('/shorten', (req, res) => {
if (err) {
return res.status(500).send(err.message);
}

if (row) {
return res.status(400).send('ID already exists');
}
Expand All @@ -63,12 +73,12 @@ app.post('/shorten', (req, res) => {
return res.status(500).send(err.message);
}

db.run("INSERT INTO urls (target_url, custom_id, password_hash) VALUES (?, ?, ?)", [targetUrl, customId, hash], function(err) {
db.run("INSERT INTO urls (target_url, custom_id, password_hash) VALUES (?, ?, ?)", [formatUrl(targetUrl), customId, hash], function(err) {
if (err) {
return res.status(500).send(err.message);
}

res.json({url: targetUrl, id: customId, password: password});
res.json({url: formatUrl(targetUrl), id: customId, password: password});
});
});
});
Expand All @@ -82,6 +92,7 @@ app.get('/:id', (req, res) => {
if (err || !row) {
return res.status(404).sendFile(path.join(__dirname, "/public/status", "404.html"));
}

res.redirect(row.target_url);
});
});
Expand All @@ -106,6 +117,7 @@ app.delete('/:id', (req, res) => {
if (err) {
return res.status(500).send(err.message);
}

if (!isMatch) {
return res.status(403).send('Invalid password\n');
}
Expand All @@ -115,11 +127,22 @@ app.delete('/:id', (req, res) => {
if (err) {
return res.status(500).send(err.message);
}

res.send('Short URL deleted successfully\n');
});
});
});
});

function formatUrl(url) {
url = url.trim();

if (!/^https?:\/\//i.test(url)) {
url = 'https://' + url;
}

return url;
}

// 啟動伺服器
app.listen(PORT, () => console.log("Server is now running..."));

0 comments on commit bd55bfd

Please sign in to comment.