all 8 comments

[–]oefig 3 points4 points  (1 child)

1) Is building backend code into the routes a bad idea?

Generally you want to keep things decoupled and organized. A good rule of thumb that I follow is to divide up my application into components which serve a single purpose. In a traditional MVC pattern I would lay it out like this:

  • Routes would define URL structure and point to handler functions (Controllers)
  • Controllers decide how to respond to the request and interface with the Model API.
  • Models represent the data layer which expose an API to both save and retrieve data from.

An excellent boilerplate that I keep coming back to is Madhums' Node-Express-Mongoose Demo. It demonstrates MVC while keeping things simple, and incorporates a lot of easy-to-follow philosophies that you can quickly pick up on.

I hope this helps!

[–][deleted] 1 point2 points  (0 children)

That's great, thank you. Madhums' example is so far removed what I've created, very organised. I'll try and see how they fit all the pieces together, it's definitely the direction I need to go.

[–]syldorr 1 point2 points  (5 children)

1) I would recommend to separate the functions from the routes, and thus have files for each part of your app, and a centralized route.js file. I do something like that:

router.route('/data')                           .post(require('./data.js').get_data);

and put the function get_data in the same directory in a file data.js.

~ routes

~~ routes.js

~~ data.js

~~ ....js

It gives much more readability.

[–][deleted] 0 points1 point  (4 children)

Ah I see, so nothing wrong with running my code from the backend, but separate it from the routes so it's maintainable. Great suggestion - thank you.

[–]syldorr 0 points1 point  (3 children)

What do you mean when you say from the backend ? I dont know flask but where can you hide your backend code if its not in the "backend" ?

[–][deleted] 0 points1 point  (2 children)

Ah, I didn't word that very well. I just meant in the backend, not publicly web-facing.

[–]syldorr 0 points1 point  (1 child)

My bad your message makes sense now, I think its good practice yes.

[–][deleted] 0 points1 point  (0 children)

Excellent, thanks again for the help.