How do I disable jade/template engine in express 4

user3844244 picture user3844244 · Aug 12, 2014 · Viewed 9k times · Source

I'm new to express and I want to explore workings of Express.

I am using latest version of express 4 (4.8.1)

I don't want to use any template engine. I want to serve my HTML content manually.

How do I do that? I searched a few questions here is StackOverflow and I found a solution. All I am supposed to do is to comment out following lines.

// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');

But when I do it, I get following error when I hit my server from the browser.

Error: No default engine was specified and no extension was provided.
   at new View (/Users/adi/Desktop/srserver/node_modules/express/lib/view.js:41:42)
   at Function.app.render (/Users/adi/Desktop/srserver/node_modules/express/lib/application.js:499:12)
   at ServerResponse.res.render (/Users/adi/Desktop/srserver/node_modules/express/lib/response.js:955:7)
   at module.exports (/Users/adi/Desktop/srserver/app.js:50:9)
   at Layer.handle_error (/Users/adi/Desktop/srserver/node_modules/express/lib/router/layer.js:52:5)
   at trim_prefix (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:261:13)
   at /Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:230:9
   at Function.proto.process_params (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:305:12)
   at /Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:221:12
   at Function.match_layer (/Users/adi/Desktop/srserver/node_modules/express/lib/router/index.js:288:3)

Here is the entire code. (app.js)

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;

app.listen(3000,function(){
    console.log("Listening on #3000");
});

app.get("/",function(req,res){
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Simple Simple Fun')
    response.end();
});

Also, I removed jade from package.json

Answer

Kfir Erez picture Kfir Erez · Apr 21, 2016

One thing is missing in the answers above.

Express complaint about render calls so all you need to do is:

  1. Commenting the view as you did correctly (also as Gerard Simpson mentioned)
  2. Replace all render calls:

From

res.render('error', {
        message: err.message,
        error: err
    });

to

res.status(500).json({
        message: err.message,
        error: err
    });