0

I am trying to render a static HTML page when a GET request is made to the root directory (/).

However, I receive the following error when accessing the root directory (http://localhost:3000/):

TypeError: Cannot read property '_locals' of undefined

Project Structure

index.js
node_modules
public
|-- html
|   |--index.html

index.js

var express = require('express');
var app = express();
var port = 3000;

app.use(express.static(__dirname + '/public'));

app.listen(port);
console.log('Server running on port ' + port + '.');

app.get('/', function(req, res) {
    app.render(__dirname + '/html/index.html');
})

I can access my static HTML page by going to http://localhost:3000/html/index.html. However, calls to app.render() fail. I suspect it has something to do with the directory I am passing into app.render().

1
  • Use res.sendFile instead of app.render. I think express is to render html files directly. Commented Nov 24, 2016 at 6:39

3 Answers 3

2
  1. You shouldn't use app.render, it should be res.render
  2. res.render is supposed to render content with template engine, for example, mustache, ejs, jade, etc. You shouldn't use it to render pure html files.
  3. if you want to send *.html content, try to use res.sendFile

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/html/index.html');
});
Sign up to request clarification or add additional context in comments.

Comments

0

Just remove this

app.get('/', function(req, res) {
    app.render(__dirname + '/html/index.html');
});

Try using it again.

Comments

0
app.get('/', function(req, res) {
    res.render(__dirname + '/html/index.html');
})

res rather than app in the function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.