-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (52 loc) · 1.62 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const express = require('express');
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));
const getConnection = require('./secret/password.js');
app.post('/url',function(req,res){
let key = makeid(10);
let data = req.body.data;
if(data == []){
res.sendStatus(400);
return;
}
getConnection((connection)=>{
connection.query('INSERT INTO url(url, data) VALUES(?,?);',[key,data],function(error, results, fields){
if(error){
console.log(error);
res.sendStatus(500);
}else{
res.send(key);
}
});
connection.release();
})
});
app.get('/:id',function(req,res){
getConnection((connection)=>{
connection.query('SELECT data FROM url WHERE url = ?;',[req.params.id],function(error, results, fields){
if(error){
console.log(error);
res.sendStatus(500);
}else{
if(results.length>0){
res.redirect(results[0].data);
}else{
res.sendStatus(404);
}
}
});
connection.release();
})
})
app.listen(11001);
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}