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 = ;
There is some basic configuration options (see Director Configuration), the below shows the minimum that has to be set.
Router;Router;Router;app;
And that's it you are now able to use Director as your express router.
Creating a route
- 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
moduleexports ="/" : "home"
- 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
moduleexports ="view": "home""controller": "home""function": "load"
This file sets up the related view, controller and the default function to run on this controller.
- 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 = moduleexports = ;homeController{return message:"Hello";}
- 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;
views/home.js
htmlheadtitlebodyh1= resultmessage
This controller is then useable through out the system, and is responsible for sending data to the views.