Follow @endyourif rss Facebook LinkedIn

Setup routes in Node.js with Express.js and Jade with controllers, models, and views Setup routes in Node.js with Express.js and Jade with controllers, models, and views

Published on Jul 14, 2019

I like the structure of Model View Controller (MVC) and I wanted to apply it to my Node.js projects.

I've written previous articles about Node using Jade Templates with Express, but I didn't go into much details about code organization; let's do that now.

MVC implementation with Node.js

Structure wise, here is my preference:

  • /

    • config
    • controllers
    • models
    • public

      • css

    • views

      • controllerName

        • index.jade

  • app.js
  • routes.js

In the above structure, I create one file per controller, model, and a sub folder in the views directory to hold my Jade jQuery template.

App.js and Route.js Setup

Example app.js setup


var express = require('express');
var app = module.exports = express.createServer();
var config = require('./config/config');
// Configuration, defaults to jade as the view engine
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.listen(80, function(){
console.log("Express server listening on port 80");
});
module.exports.app = app;
routes = require('./routes');

The above code sets up Express and Jade as the templating engine, initializes the web server and loads the routes.js file.

Example routes.js setup


app = module.parent.exports.app;
/* require your controllers here */
var homeController = require('./controllers/home');
var userController = require('./controllers/user');
app.get('/', homeController.index);
app.get('/users', userController.index);
app.get('/users/detail', userController.detail);

This instantiates two controllers: home and user. Then one route is created for / that calls the index function in the home controller and two routes are created for the user controller: index and detail.

Hopefully this is a good structure to get you started with using MVC, Express.js, and Jade templates.

Tags: Node js Tutorial | express | jade

About Jamie Munro

Welcome to my blog. I have been compiling articles since 2009! Below is a list of every post I've ever created. Some of my major focuses have been compiling the best source of jQuery tutorials on the Internet today. A recent focus of mine is now focusing on creating the best SQL Tutorials for Beginners, Intermediate and Advanced Users. This is a compiliation of all my experience about how to do common SQL commands to optimize for performance. As you might see from the links around here, I've also written and published 5 books. My first and last book are focused around ASP.NET MVC and Web API tutorials. The last book has a big focus on integrating Knockout.js as the MVVM for the front-end code. O'Reilly Media was so interested in Knockout.js that they actually commissioned me to write that book first as it is one of the only books dedicated entirely to this MVVM library. So if you're looking for the best Knockout.js tutorials, I've compiled the best list. My first book, even though it was self published, was on the PHP framework: CakePHP. I used this framework for years and many of my first ever exampes were all focused on CakePHP tutorials that provide so many fantastic PHP examples. There are so many Javascript tutorials out there, but I noticed that one of the things I work on most with Javascript is arrays. With this I've worked hard on compiling a big list of really advanced Javascript array examples. These array examples are amazing including how to group by, get distinct array elements and so many more. I've also dabbled a little bit with Node.js and during those experiments I definitely ran into some common errors like Can't set headers after they are sent, Solving No Access-Control-Allow-Origin with Node js and Express, and Uncaught ReferenceError: require is not defined. With these common errors I've compiled a list of Node.js tutorials that help solve these problems, but more importantly getting you started with the basics.