node.jsrouteslocomotivecmslocomotivejs

Routing in locomotive using ejs


I'm trying out node and some frameworks for node atm, specifically locomotive. However, i seem to be stuck on routing using locomotive. A couple questions i can't find the answer to, so here goes:

  1. why does the locomotive out-of-box install use index.html.ejs as a filename? Why not just index.ejs? What's the benefit?

  2. i'm trying to add a route to a view: searchName.html.ejs which i added in the views folder. To achieve this i made a toolController like this:

    var locomotive = require('locomotive').Controller,
    toolController = new Controller();
    
    toolController.searchName = function() {
        this.render();
    }
    
    module.exports = toolController;
    

    I also added a route in routes.js like so:

    this.match('searchName', 'tool#searchName');
    

    However, that doesn't work (and yet it's what the documentation says ought to work). The result is a 404 error. So how do i make that route work?

  3. Suppose i want to make a route to eg, anExample.html? How do i go about that? I notice that in the out-of-the-box app from locomotive, you cannot enter localhost:3000/index.html . Nor even localhost:3000/index This seems highly impractical to me, as there are plenty of users who'll add the specific page they want to go to. So how can i make that work?

PS: I went through all questions regarding this on stackoverflow and searched the web, but i still can't figure this out.enter code here


Solution

    1. The benefit is that this naming scheme allows you to specify several different formats for a single route. So you could have search_name.html.ejs and search_name.xml.ejs, then respond with either view depending on what your client is expecting.

    2. There are a couple issues with the example code you posted. You should be seeing a more descriptive error than a 404, so I'm not sure what's happening there, but here are the fixes to your code that work in my environment.

      In the controller:

      //tool_controller.js
      var locomotive = require('locomotive');
      var toolController = new locomotive.Controller();
      
      toolController.searchName = function() {
        this.render();
      };
      
      module.exports = toolController;
      

      In routes.js:

      //routes.js
      module.exports = function routes()
      {
        this.match('searchName', 'tool#searchName');
      }
      

      Then, you'll need to change the view to this: views/tool/search_name.html.ejs. It's not clear from the documentation, but locomotive automatically lowercases and underscores actions that are camel-cased, like searchName.

      Now start the app and browse to http://localhost:3000/searchName

    3. If you just want to serve a static html file, the easiest way is to just drop it in the public folder. This folder is specifically for serving up static content like client-side js, css, etc. And it works just fine for serving static HTML as well.