0

I am trying to use EJS but whenever res.render is called, it just hangs there with no response.

This is my app.js file

var express = require('express');
var bodyParser = require('body-parser')
var mongoose = require('mongoose');
var app = express();
var router = require('./router/index')(app);
app.use(bodyParser.json())
app.set('view engine', 'ejs');
// Internal Server error
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
});

module.exports = app;

app.listen(8888);

And in my routes file

module.exports = function (app) {
  app.use('/', require('./routes/Website'));
  app.use('/api/users', require('./routes/UsersRoute'));
  app.use('/api/keys', require('./routes/KeysRoute'));
};

And in the website file

var express = require('express');
var router = express.Router();

// Get all Users data
router.get('/', function (req, res) {
  res.render('index', { title: 'The index page!' })
});

I have a index.eps file in views folder and a layout.eps in the layout folder. What am i doing wrong in this?

Exceptions
  • 1,174
  • 2
  • 9
  • 28
  • make it index.ejs .EJS use .ejs extension to save file. – PPB Jun 05 '15 at 04:44
  • My files are saved in .ejs and I've tried using `res.render('index.ejs', { title: 'The index page!' })` However the problem still persists – Exceptions Jun 05 '15 at 04:53

2 Answers2

0

Should your internal server error middleware have a conditional?? It seems to me that it would try to send the response from your middleware every time a route is called.

Try commenting out the res in your middleware and try calling next();

Datsik
  • 14,453
  • 14
  • 80
  • 121
  • Thanks this solved the hanging issue but I'm getting `Cannot GET /` message now. My els file is in views folder. – Exceptions Jun 05 '15 at 05:40
0

Solved the problem by removing

// Internal Server error
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
});

And seems like EJS looks for views folder in the root directory instead of the directory of the .js file

Exceptions
  • 1,174
  • 2
  • 9
  • 28