Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate the routes and models from app.js using NodeJS and Express

I'm creating an app using Node and Express. However, I can see it'll soon become difficult to manage all the routes that are placed inside app.js. I have placed all my models in a subdirectory /models.

Here's my app current structure:

app.js models   -- products   -- customers   -- ... public views node_modules 

In app.js:

var express = require('express'),     routes = require('./routes'),     user = require('./routes/user'),     http = require('http'),     path = require('path'),     EmployeeProvider = require('./models/employeeprovider').EmployeeProvider,     Products = require('./models/products').Products,     Orders = require('./models/orders').Orders,     Customers = require('./models/customers').Customers,     checkAuth = function(req, res, next) {       if (!req.session.user_id) {         res.send('You are not authorized to view this page');       } else {         next();       }     };  var app = express(); 

Then some configuration like port, views directory, rendering engine, etc.

Further down app.js I've got the routes:

app.get('/product/edit', auth, function(req, res) {   Products.findAll(function(error, prds) {     res.render('product_edit', {       title: 'New Product',       products: prds     });   }); }); 

At the top I'm assigning the contents of models/products.js to a variable, all works fine. However keeping all routes inside app.js is not ideal. But if I move the routes to routes/product.js and load the Products models:

var prod = require('../models/products.js'); 

I get an error saying that object has no method findAll.

What am I doing wrong? How can I remove the routes from app.js?

like image 806
WagnerMatosUK Avatar asked Oct 27 '13 15:10

WagnerMatosUK


People also ask

Why should you separate Express app and server in NodeJS?

Faster testing execution. Getting wider coverage metrics of the code. Allows deploying the same API under flexible and different network conditions. Better separation of concerns and cleaner code.

How can we create Chainable route handlers for a route path in Expressjs app?

Answer: A is the correct option. By using app. route() method, we can create chainable route handlers for a route path in Express.

What is a route in NodeJS and Express?

Routing refers to how an application's endpoints (URIs) respond to client requests.


1 Answers

As of express 4.x Router is added to support your case.

A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

Example from expressjs site:

// routes/calendarRouter.js  var express  = require('express'); var router = express.Router();  // invoked for any requested passed to this router router.use(function(req, res, next) {   // .. some logic here .. like any other middleware   next(); });  // will handle any request that ends in /events // depends on where the router is "use()'d" router.get('/events', function(req, res, next) {   // .. });  module.exports = router; 

Then in app.js:

// skipping part that sets up app  var calendarRouter = require('./routes/calendarRouter');  // only requests to /calendar/* will be sent to our "router" app.use('/calendar', calendarRouter);  // rest of logic 
like image 193
Ivar Avatar answered Sep 23 '22 23:09

Ivar