Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics (2015) Using Templates with Express Scaffolding Your Project’s Templates - adding β€œpartials”

Sebastian Andersson
seal-mask
.a{fill-rule:evenodd;}techdegree
Sebastian Andersson
Full Stack JavaScript Techdegree Student 13,003 Points

Res.render post variable not working. Also './' url duplicating its contents.

Hi! I am having some trouble with the scaffolding of the project.

I have the layout.jade, index.jade. post.jade, _head.jade and _nav.jade files currently in my project and they seem to be connected in some sense but not completely.

The path of 'localhost:7000/' is duplicating its tags, the 'localhost:7000/posts' seem to be working as it should, with the error message. However the 'localhost:7000/posts/TITLE is only displaying the nav bar and not the content. If I switch the variables to just regular text the same problem as in the localhost:7000 appears. That is, duplicate tags.

app.js file:

var express = require("express"),
    posts = require("./mock/posts.json");

var app = express();

var port = 7000;

app.set('view engine', 'jade');
app.set('views', __dirname + '/templates');

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

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

app.get('/posts/:title?', function(req, res ){
    var title = req.params.title;
    if (title === undefined) {
        res.status(503);
        res.send("This page is under construction");
    } else {
        var post = posts[title] || {};
        res.render('post', {post: post});
    }
});

app.listen(port, function(){
    console.log("Frontend server running on port " + port);
});

layout.jade file:

doctype html
html(lang="en")
    include ./partials/_head.jade

    body

        include ./partials/_nav.jade
        block content

index.jade file:

extends ./layout.jade
block content

    h1 The future home, of something awesome!
    p Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consectetur quis, hic similique! Hic ea tempora, dolores esse, incidunt quod laborum reiciendis nihil consequuntur perspiciatis debitis, soluta explicabo. Magni, sunt, ipsum. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis optio praesentium necessitatibus explicabo voluptate? Deleniti repellat culpa, quos excepturi minus totam consequatur, nobis ullam quidem est modi maiores saepe commodi?

    h2 Do you want to build a snowman?
    p Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem aut officiis cupiditate quis, numquam quas soluta quasi incidunt ut, ex odio provident iure pariatur. Quas nobis officia excepturi qui sit.

post.jade file:

extends ./layout.jade
block content

    h1 #{post.title}

    article #{post.description}

_nav.jade file:

nav 
    p - THIS IS THE NAV -
    block content

_head.jade file:

head
    title
    // All static goes here

2 Answers

Dimitrius Ionov
Dimitrius Ionov
17,737 Points

The path of 'localhost:7000/' is duplicating its tags because you have block content at the end of your nav bar or _nav.jade and in your layout.jade

Dimitrius Ionov
Dimitrius Ionov
17,737 Points

'localhost:7000/posts/Test renders correctly for me with posts.json that looks like this: { "Test": { "title": "TEST", "description": "SOME TEXT" } }