Nunjucks does not render template

I have a folder structure like this:

/app
   |-routes.js
/public 
   |-index.html
server.js

      

Server.js looks like this:

var nunjucks = require('nunjucks');
var express        = require('express');
var app            = express();
nunjucks.configure('public', { autoescape: true, express: app });
var port = process.env.PORT || 8080; 
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // configure our routes
app.listen(port);               
exports = module.exports = app;

      

app / routes.js looks like this:

module.exports = function(app) {
        app.get('*', function(req, res) {
            res.render('index.html', { awesome: 'page-title' }); 
        });
    };

      

public / index.html looks like this:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">

    <title>{{awesome}}</title>

</head>
<body>
        <h1>{{ awesome }}</h1>    
    </div>
</body>
</html>

      

When I start my node application and go to localhost: 8080, the page title is {{awesome}} and contains the line {{awesome}}, not the desired "page title". Why is nunjucks not passing a variable to the template?

+3


source to share


2 answers


You need to share the "public" folder. What is open cannot be handled by nunjucks. It's static (you stated it). Place the nunjucks template in a folder eg. 'View'



/app
   |-routes.js
/public 
   |css/
   |img/
/views
   |- index.html
server.js

nunjucks.configure('views', ...)

      

+3


source


if you are using thinkjs for windows: your view.js should look like this:



export default {type: 'nunjucks',root_path: think.ROOT_PATH + '\\view',};

0


source







All Articles