Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Get started #1

wants to merge 4 commits into from

Conversation

AnshulMalik
Copy link
Collaborator

@AnshulMalik AnshulMalik commented Aug 15, 2017

@nkkumawat 's first PR

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a gitignore file

app.js Outdated
}
});
app.get('/speaker' , function (req , res) {
res.sendFile(__dirname + '/www/speaker.html');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

https://expressjs.com/en/starter/static-files.html

app.js Outdated
var post = req.body;
var username = post.user;
var password = post.password;
var sql = "SELECT * FROM login WHERE username='"+username+"'";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should play safe here, you must have heard of SQL Injection. @rewanth1997 knows better.
Let's sanitize user input before sending it to mysql.

app.js Outdated
var name = post.speakername;
var topic = post.topic;
var description = post.description;
var sql = "INSERT INTO speaker(name, topic, description, pic_url) values('"+name+"','"+topic+"','"+description+"','/images/"+post.images+"')";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same sanitization logic applies here.

app.js Outdated
});
}
// Starting Server
server.listen(3000, function(){
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need this port to be configurable from environment variables, so can use

const port = process.env.PORT || 3000;
server.listen(port, () => ...)

app.js Outdated
var name = post.speakername;
var topic = post.topic;
var description = post.description;
var sql = "INSERT INTO speaker(name, topic, description, pic_url) values('"+name+"','"+topic+"','"+description+"','/images/"+post.images+"')";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template literals make the code cleaner, we can use those here.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

package.json Outdated
"description": "backend for tedx nit k",
"main": "app.js",
"scripts": {
"test": "node app.js"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this command be start instead of test?

});
</script>
</body>
</html>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every file must contain an empty line at the end :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

app.js Outdated
@@ -9,6 +9,8 @@ var bodyParser = require('body-parser');
var session= require('express-session');
var multer = require('multer');
var mysql = require('mysql');
var sanitizeHtml = require('sanitize-html');
Copy link
Collaborator Author

@AnshulMalik AnshulMalik Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool, we'd also like to prevent sql injections

app.js Outdated
});
}
function insertVideo(req , res ) {
var post = req.body;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use let and const instead of var

Copy link
Member

@rewanthtammana rewanthtammana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice start @nkkumawat.

Here are a few tips:

  1. Make sure you follow the same style of coding in all your files.
  2. Assign appropriate names to the API endpoints and other variables.
  3. Use appropriate commit messages, so that it will be easy for us to review your code.

Do not push multiple parts of code into a single commit message like this, "sanitizition before sql injection, env ports add , image upload done". This is a very bad way to do commit messages.

Instead prefer using single commit message for each change, it makes easy for us to review the code. Before merging them into the master branch we can squash the commits to make the code look more elegant.

app.js Outdated
resave: true,
saveUninitialized: true
}));
var Storage = multer.diskStorage({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in variable starting with Caps letter? Any special reason, if not use small case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for any special reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repalced

app.js Outdated
});
});
app.post("/videoinsert", function(req, res) {
insertVideo(req , res);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer not to use create functions if you are not reusing the code. Insert the code here itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You used the same style of coding everywhere. Change it wherever required.

app.js Outdated
insertVideo(req , res);
});
function login(req,res){
var post = req.body;
Copy link
Member

@rewanthtammana rewanthtammana Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use appropriate names is wherever required. Its the standard way of coding.

const body = req.body

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

app.js Outdated
var url = post.url;
title = sanitizeHtml(title);
description = sanitizeHtml(description);
var sql = "INSERT INTO videos(title, description, video_url) values('"+title+"','"+description+"','"+url+"')";
Copy link
Member

@rewanthtammana rewanthtammana Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url variable is not sanitized. This type of SQL query is easily prone to attacks. Make sure to sanitize each and every parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"path": "^0.12.7",
"sanitize-html": "^1.14.1"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new lines at EOF, asap.

color: white;
font-family: hBold;
border: 1px solid rgba(0, 0, 0, 0.1);
}
Copy link
Member

@rewanthtammana rewanthtammana Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a common mistake I observed in most of your code. If you plan to use \n after each block, follow same style of coding in all your files. Here there is no newline after the block of code but at lines 34, 38, etc.. you have used new line. Better use newline after each and every block of code.

Apply this fix to all the other files.

var pass = $("#pass");
var error = $("#error");
$(".btn").click(function(){
console.log(pass.val());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to delete these kind of console logs after testing, which expose sensitive data to the end user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure sir

www/login.html Outdated
var error = $("#error");
$(".btn").click(function(){
console.log(pass.val());
var data = {"user": ""+user.val()+"" , "password":""+ pass.val()+""};
Copy link
Member

@rewanthtammana rewanthtammana Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this inline code so complex? Instead you can use an elegant code like this,

const data = {
    "user" : user.val(),
    "password" : pass.val()
}

Try to make your code as simple as possible.

app.js Outdated
app.get('/speaker' , function (req , res) {
res.sendFile(__dirname + '/www/speaker.html');
});
app.post('/speaker' , function (req , res) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are returning the list of all speakers, the endpoint must be '/speakers' instead of '/speaker'.

If you are returning details of only one speaker then you can use '/speaker'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

app.js Outdated
res.redirect("/login");
});
app.post('/getuser' , function (req , res) {
res.send({"username" : req.session.user});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If req.session.user returns only username then change req.session.user to req.session.username.
If req.session.user returns any extra data apart from username then change "username" to "user".

Make sure you keep the naming conventions appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username 👍 )

app.js Outdated
console.log(req.session.user);
if(!req.session.user) {
console.log("Admin");
if(!req.session.username) {
res.redirect('/login');
Copy link
Collaborator Author

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.

app.js Outdated
res.sendFile(__dirname + '/www/speaker.html');
});
app.post('/speaker' , function (req , res) {
app.post('/speakers' , function (req , res) {
Copy link
Collaborator Author

@AnshulMalik AnshulMalik Aug 18, 2017

Choose a reason for hiding this comment

The 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 /api/* route.

app.js Outdated
});
});
app.post("/speakerinsert", function(req, res) {
app.post("/speaker-insert", function(req, res) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a POST request to /speakers.

app.js Outdated
res.send({"username" : req.session.user});
res.send({
"username" : req.session.username
});
});
app.get('/blog' , function (req , res) {
Copy link
Collaborator Author

@AnshulMalik AnshulMalik Aug 18, 2017

Choose a reason for hiding this comment

The 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.

app.js Outdated
description = sanitizeHtml(description);
var sql = "INSERT INTO videos(title, description, video_url) values('"+title+"','"+description+"','"+url+"')";
});
app.post("/delete-video", function(req, res) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we make this DELETE instead of POST and URL to be /video/<id>.

app.post("/delete-video", function(req, res) {
const post = req.body;
const videoTitle = sanitizeHtml(post.videoTitle);
var sql = "Delete from videos where title = '" + videoTitle +"'" ;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would probably want to add some auth here in future.

app.js Outdated
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');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants