trailsjs / trailpack-express

:package: Express Trailpack
MIT License
12 stars 15 forks source link

Error: Failed to lookup view "index" in views directory #39

Closed connor11528 closed 8 years ago

connor11528 commented 8 years ago

I am building an application to render views with pug. I installed pug (npm i pug --save) and set up config/views.js:

module.exports = {
  engine: 'pug'
}

Then my config/web.js I set up like this with express packages:

'use strict'

var bodyParser = require('body-parser');

/**
 * Server Configuration
 * (app.config.web)
 *
 * Configure the Web Server
 *
 * @see {@link http://trailsjs.io/doc/config/web}
 */
module.exports = {
  express: require('express'),

  /**
   * CORS options
   * Can be true/false or an object of CORS options
   * @see {@link https://github.com/expressjs/cors#configuring-cors}
   */
  cors: false,

  /**
   * Middlewares to load (in order)
   */
  middlewares: {

    //middlewares loading order
    order: [
      'addMethods',
      'cookieParser',
      'bodyParser',
      'methodOverride',
      'www',
      'router',
      '404',
      '500'
    ],

    // Middlewares to load for body parsing
    bodyParser: [
      bodyParser.json(),
      bodyParser.urlencoded({extended: false})
    ]

  },

  cache: 31557600000,

  /**
   * The host to bind the web server to
   */
  //host: process.env.HOST || 'localhost',

  /**
   * The port to bind the web server to
   */
  port: process.env.PORT || 3000

}

Then created views/index.pug

html
  head
    title= title
  body
    h1= message

In ViewController (api/controllers/ViewController.js) I changed the line to render index.

const Controller = require('trails-controller')

module.exports = class ViewController extends Controller {
  helloWorld(req, res) {
    res.render('index')
  }
}

I get an error that it fails to look up the index file! What am I missing here to render the template?

That you very much! Error message below

trails > error:  Error: Failed to lookup view "index" in views directory "/Users/connorleech/Projects/trails-tutorial/views"
    at EventEmitter.render (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/application.js:579:17)
    at ServerResponse.render (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/response.js:960:7)
    at ViewController.helloWorld (/Users/connorleech/Projects/trails-tutorial/api/controllers/ViewController.js:7:9)
    at Layer.handle [as handle_request] (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/route.js:131:13)
    at args.(anonymous function) (/Users/connorleech/Projects/trails-tutorial/node_modules/trailpack-express/lib/utils.js:21:13)
    at Layer.handle [as handle_request] (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at /Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:176:3)
    at router (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:46:12)
    at Layer.handle [as handle_request] (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:312:13)
    at /Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/connorleech/Projects/trails-tutorial/node_modules/express/lib/router/index.js:271:10)
    at SendStream.error (/Users/connorleech/Projects/trails-tutorial/node_modules/serve-static/index.js:121:7)
    at emitOne (events.js:96:13)
    at SendStream.emit (events.js:188:7)
    at SendStream.error (/Users/connorleech/Projects/trails-tutorial/node_modules/send/index.js:275:17)
    at SendStream.onStatError (/Users/connorleech/Projects/trails-tutorial/node_modules/send/index.js:392:12)
    at next (/Users/connorleech/Projects/trails-tutorial/node_modules/send/index.js:711:28)
    at /Users/connorleech/Projects/trails-tutorial/node_modules/send/index.js:719:23
    at FSReqWrap.oncomplete (fs.js:117:15)
error: Error sending page 500, maybe you don't have a 500.html 
...

This is the repo for the code that I am building: https://github.com/connor11528/trails-tutorial

Thank you!

jaumard commented 8 years ago

When you use config/views.js the template use the html extension. So your file have to be index.html :)

connor11528 commented 8 years ago

Oh cool thank you! I changed the file to views/index.html and it worked!