Monday 12 June 2017

[QUESTION] Is my API routing logic bad?

Hi! Building my first non-trivial express api to be consumed by an Angular2 app. I'm having a real hard time with modelling. I want to use mysql, not mongo, so I'm also having trouble finding good tutorials.Basically, my logic is as follows:api/users.jsrouter.get('/',function(req,res){ pool.getConnection(function(err,connection){ if(err) return res.json(err); else { connection.query("select id,name,privilege,max,holding,max_visiting,visiting from users",function(err,rows){ //GET ALL USERS if(err) return res.json(err); else return res.json(rows); (...) I know this is not standard. From what I understand, best practice is to have a class "User" declared somewhere, with methods that will access the database and return the content. On the routing page, I'll just have to create an instance of that class, call the method and return the results.What I don't understand is WHY that is best practice. It justs seems a lot more code for no good reason. Am I missing something here? Is my code that bad? Thanks!

Submitted June 12, 2017 at 04:04PM by fariswyatt

No comments:

Post a Comment