director-js

1.0.2 • Public • Published

Director

An express middlewear router, that is based around controllers.


Install Director

Simply use npm to install the Director module

  npm install director

Using Director

In your server.js file require the module

  
  const Router = require("./director");
  

There is some basic configuration options (see Director Configuration), the below shows the minimum that has to be set.

 
  Router.set("urls","./routes.js");
  Router.set("routes","./routes");
  Router.set("controllers","./controllers");
  
  app.use('/', Router.router);
 

And that's it you are now able to use Director as your express router.


Creating a route

  1. Edit your routes file (the first file set above). This is a key value pair file that maps a url to a route configuration file.

./routes.js

  
  module.exports = {
    "/" : "home"
  }
 
  1. Create the corrosponding routes file in the routes directory, there are more options we can specify here see I'm an inline-style link

./routes/home.js

 
  module.exports = {
    "view": "home",
    "controller": "home",
    "function": "load"
  }
 

This file sets up the related view, controller and the default function to run on this controller.

  1. Create the controller

The next step is to create a controller file, this is where your logic for this route will go.

./controllers/home.js

  var homeController = module.exports = { };
  
  homeController.load = function(){
    return {message:"Hello"};
  }
 
  1. Create a view

The final step is to create a normal express view, this should be named the same as the view option in the routes config file. We can use any templating engine we want as this is still controlled by express.

server.js

  app.set('view engine', 'pug');

views/home.js

  html
    head
      title
    body
      h1= result.message
 

This controller is then useable through out the system, and is responsible for sending data to the views.

Package Sidebar

Install

npm i director-js

Weekly Downloads

0

Version

1.0.2

License

ISC

Last publish

Collaborators

  • tvance1