Wednesday, 26 April 2017

How to use routes properly in node.js?

I have app architecture(approximately): (I'm using Express framework)

-routes
-views
-public
-app.js

I tried to use a route in app.js in this way:

var router = require('routes');
app.get('/', router.get);
app.get('/otherRoute', router.get);

File index.js in routes folder:

exports.get = function(req, res) {
   res.render('index',{
       title: "Express",
       a: '<a href="#">Link</a>'
   });
};

I generated only this route '/', '/otherRoute' also generated by index.js of course. I just show the schema.

  • How I can use routes in my app properly? I want, for example, that routes handle by separate file (routes/index.js, routes/about.js, ...). Or will be better to use a one file to handle routes?
  • What app architecture I should have?
  • Hint me, if you know, materials(articles,books) for how I can build node.js apps.

Thank you.



via user7103883

No comments:

Post a Comment