Skip to content

Commit

Permalink
URL로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkyme committed Mar 9, 2022
1 parent 3b1bcdc commit bdfcabd
Show file tree
Hide file tree
Showing 4 changed files with 762 additions and 117 deletions.
46 changes: 27 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,45 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));

var connection = require('./secret/password');
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;
}
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);
}
});
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){
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.send(results[0].data);
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{
res.sendStatus(404);
if(results.length>0){
res.redirect(results[0].data);
}else{
res.sendStatus(404);
}
}
}
});
});
connection.release();
})

})

app.listen(11001);
Expand Down
Loading

0 comments on commit bdfcabd

Please sign in to comment.