How do I disable jade/template engine in express 4
Asked Answered
S

5

9

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

Seoul answered 12/8, 2014 at 14:36 Comment(2)
possible duplicate of Render basic HTML view in Node JS Express?Cinquain
This question was answered here: #17911728Loferski
L
17

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
    });
Lach answered 21/4, 2016 at 15:12 Comment(0)
C
5

Commenting out this code is correct:

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

The reason that you are getting this error, is because you are not supplying a file with a file name extension in your ./public directory. The error has a second part that says:

no extension was provided

The only piece of code that you need for your express 4 server to server static html files is this:

app.use(express.static(path.join(__dirname, 'public')));

Which tells your express server to look in your ./public directory to serve static files.

If you add ./public/index.html, you will be able to serve it as a static file.

Hope this helps

Curarize answered 19/3, 2016 at 8:11 Comment(0)
A
1

you have two options:

  1. using template engine Jade, ejs, etc.. and using res.render meaning render the template
  2. use only static file and api calls than you just call the api and render your view on the client side with the json that got return from the server. res.send({users: [{id:1, name: 'adi'},{id:2, name: 'dave']});

if you're trying to use express for something like Apache or nginx I think you should just use https://github.com/yeoman/generator-webapp

Alexandro answered 11/3, 2015 at 19:22 Comment(0)
B
0

Use the following to send the html file directly

 res.sendFile('path/toFile')

You might also take a look at the api reference for Details about the method.

This thread also mentions some alternatives like letting express serve your html as static files.

Boysenberry answered 2/3, 2016 at 11:2 Comment(0)
M
0

I know that is an old question, but I get some error.

You need to change what @Kfir Erez said and also change the response in your routes/index.js:

router.get('/', function(req, res, next) {
   res.render('index', { title: 'Express' });
});

to something like:

router.get('/', function(req, res, next) {
   res.send('index');
});
Melyndamem answered 16/4, 2018 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.