-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get started #1
base: master
Are you sure you want to change the base?
Get started #1
Changes from 1 commit
0ee6df3
e453857
b450342
4d5bffb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ app.use(session({ | |
resave: true, | ||
saveUninitialized: true | ||
})); | ||
var Storage = multer.diskStorage({ | ||
var storage = multer.diskStorage({ | ||
destination: function(req, file, callback) { | ||
callback(null, "./www/images"); | ||
}, | ||
|
@@ -38,113 +38,111 @@ var Storage = multer.diskStorage({ | |
} | ||
}); | ||
var upload = multer({ | ||
storage: Storage | ||
storage: storage | ||
}).array("images", 3); | ||
app.get('/login' , function (req , res) { | ||
if(req.session.user) { | ||
if(req.session.username) { | ||
res.redirect('/admin'); | ||
}else { | ||
res.sendFile(__dirname + '/www/login.html'); | ||
} | ||
}); | ||
app.post('/login' , function (req , res) { | ||
login(req , res); | ||
const post = req.body; | ||
const username = sanitizeHtml(post.user); | ||
const password = sanitizeHtml(post.password); | ||
var sql = "SELECT * FROM login WHERE username='"+username+"'"; | ||
con.query(sql, function (err, result, fields) { | ||
if( result.length > 0 && result[0].password === password ) { | ||
console.log("Auth set"); | ||
req.session.username = post.user; | ||
res.send({"result" : "Found"}); | ||
}else { | ||
res.send({"result": "NotFound"}); | ||
} | ||
}); | ||
}); | ||
app.get('/admin' , function (req , res) { | ||
console.log(req.session.user); | ||
if(!req.session.user) { | ||
console.log("Admin"); | ||
if(!req.session.username) { | ||
res.redirect('/login'); | ||
}else { | ||
res.sendFile(__dirname + '/www/admin.html'); | ||
} | ||
}); | ||
app.get('/speaker' , function (req , res) { | ||
app.get('/speakers' , function (req , res) { | ||
res.sendFile(__dirname + '/www/speaker.html'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we use Express's static feature to serve static files? It'll be much cleaner. |
||
}); | ||
app.post('/speaker' , function (req , res) { | ||
app.post('/speakers' , function (req , res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be get method, since we are getting list of speakers. Let's not merge API and static pages, we can put all api requests under |
||
var sql = "SELECT * FROM speaker "; | ||
con.query(sql, function (err, result, fields) { | ||
var jsonString = JSON.stringify(result); | ||
var jsonData = JSON.parse(jsonString); | ||
res.send(jsonData); | ||
res.send(result); | ||
}); | ||
}); | ||
app.post("/speakerinsert", function(req, res) { | ||
app.post("/speaker-insert", function(req, res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a POST request to |
||
upload(req ,res, function(err) { | ||
if (err) { | ||
return res.end("Something went wrong!"); | ||
}else { | ||
const post = req.body; | ||
const name = sanitizeHtml(post.speakername); | ||
const topic = sanitizeHtml(post.topic); | ||
const description = sanitizeHtml(post.description); | ||
var sql = "INSERT INTO speaker(name, topic, description, pic_url) " + | ||
"values('"+name+"','"+topic+"','"+description+"','/images/"+name+".jpg')"; | ||
con.query(sql, function (err, result, fields) { | ||
res.redirect('/admin'); | ||
}); | ||
} | ||
insertSpeaker(req , res); | ||
}); | ||
}); | ||
app.get('/logout', function (req, res) { | ||
req.session.destroy(); | ||
res.redirect("/login"); | ||
}); | ||
app.post('/getuser' , function (req , res) { | ||
res.send({"username" : req.session.user}); | ||
res.send({ | ||
"username" : req.session.username | ||
}); | ||
}); | ||
app.get('/blog' , function (req , res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Static pages should not be handled by api, if required, we can have a separate file for serving static files. |
||
res.sendFile(__dirname + '/www/blog.html'); | ||
}); | ||
app.post('/blog' , function (req , res) { | ||
var sql = "SELECT * FROM videos"; | ||
con.query(sql, function (err, result, fields) { | ||
var jsonString = JSON.stringify(result); | ||
var jsonData = JSON.parse(jsonString); | ||
res.send(jsonData); | ||
res.send(result); | ||
}); | ||
}); | ||
app.post("/videoinsert", function(req, res) { | ||
insertVideo(req , res); | ||
}); | ||
function login(req,res){ | ||
var post = req.body; | ||
var username = post.user; | ||
var password = post.password; | ||
username = sanitizeHtml(username); | ||
password = sanitizeHtml(password); | ||
var sql = "SELECT * FROM login WHERE username='"+username+"'"; | ||
app.post("/video-insert", function(req, res) { | ||
const post = req.body; | ||
const title = sanitizeHtml(post.title); | ||
const description = sanitizeHtml(post.description); | ||
const url = sanitizeHtml(post.url); | ||
var sql = "INSERT INTO videos(title, description, video_url) " + | ||
"values('"+title+"','"+description+"','"+url+"')"; | ||
con.query(sql, function (err, result, fields) { | ||
var jsonString = JSON.stringify(result); | ||
var jsonData = JSON.parse(jsonString); | ||
if( jsonData.length > 0 && jsonData[0].password === password ) { | ||
console.log(req.session.user + "Auth set"); | ||
req.session.user = post.user; | ||
res.send({"result" : "Found"}); | ||
}else { | ||
res.send({"result": "NotFound"}); | ||
} | ||
res.redirect('/admin'); | ||
}); | ||
} | ||
function insertSpeaker(req , res ) { | ||
var post = req.body; | ||
var name = post.speakername; | ||
var topic = post.topic; | ||
var description = post.description; | ||
name = sanitizeHtml(name); | ||
topic = sanitizeHtml(topic); | ||
description = sanitizeHtml(description); | ||
var sql = "INSERT INTO speaker(name, topic, description, pic_url) values('"+name+"','"+topic+"','"+description+"','/images/"+name+".jpg')"; | ||
}); | ||
app.post("/delete-speaker", function(req, res) { | ||
const post = req.body; | ||
const speakerName = sanitizeHtml(post.speakerName); | ||
var sql = "Delete from speaker where name = '" + speakerName +"'" ; | ||
con.query(sql, function (err, result, fields) { | ||
res.redirect('/admin'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stick with one convention, either double or single quotes. A linter will be really helpful. |
||
}); | ||
} | ||
function insertVideo(req , res ) { | ||
var post = req.body; | ||
var title = post.title; | ||
var description = post.description; | ||
var url = post.url; | ||
title = sanitizeHtml(title); | ||
description = sanitizeHtml(description); | ||
var sql = "INSERT INTO videos(title, description, video_url) values('"+title+"','"+description+"','"+url+"')"; | ||
}); | ||
app.post("/delete-video", function(req, res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we make this |
||
const post = req.body; | ||
const videoTitle = sanitizeHtml(post.videoTitle); | ||
var sql = "Delete from videos where title = '" + videoTitle +"'" ; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would probably want to add some auth here in future. |
||
con.query(sql, function (err, result, fields) { | ||
res.redirect('/admin'); | ||
}); | ||
} | ||
}); | ||
// Starting Server | ||
const port = process.env.PORT || 3000; | ||
server.listen(port, function(){ | ||
console.log('listening on *:3000'); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
"sanitize-html": "^1.14.1" | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new lines at EOF, asap. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ | |
</script> | ||
</body> | ||
</html> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can put all routes in single file, since they'll be reused everywhere.