Node.js for Rails developers, Part 4 (Express Controllers)

This post is part of a series for Rails developers who want to get started with Node.js. After configuring our application, we’re ready to add controllers and routing logic.

Creating Controllers

Let’s add the application’s controllers.

touch app/controllers/robots_controller.js

Edit the controllers using the below file templates:

// app/controllers/home_controller.jsvarexpress=require('express');varrouter=express.Router();/* GET home page. */router.get('/',function(req,res,next){console.log("VISITED THE HOME PAGE")res.redirect('/robots')});module.exports=router;

This controller logic enables basic application navigation and flash messaging, even though database connectivity has not yet been established. Each call to res.redirect() or res.render()references the name of an EJS view file. Let’s create these views now.