Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Express Basics (2015) Doing more with Express Making Lists in Jade Templates

Not getting posts object

When I run post in the console, I don't get anything. There are no Objects returned.

If I just go ahead and write out the map function, I get:

Object.keys(posts).map(function(value){return posts[value]})
TypeError: Cannot convert undefined or null to object
No Properties
Runtime.getProperties failed.
Object #null# not found

My app.js file:

'use strict';

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

var app = express(); 

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

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

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

app.get('/blog/:title?', function(req, res){
    var title = req.params.title;
    if (title === undefined){
        res.status(503);
        res.render('blog')
        } else {
        var post = posts[title] || {};
        res.render('post', { post: post}); 
        }       
});

app.listen(3000, function(){
    console.log("the frontend server is running on port 3000");
});

Overall, it's like something happened between the last set of videos and this set that changed. I'm not sure what it is. Even localhost:3000 isn't working. I get a "This webpage is not available; ERR_CONNECTION_REFUSED" error when I reload the page.

Casey Ydenberg
Casey Ydenberg
15,622 Points

If localhost:3000 isn't working, there's something deeper wrong. Is your App running when you make the request? Can you see the requests coming in through the console? Remember, Node is not like Ruby or PHP or Python. The program has to be running all the time.

Hi Casey,

Sorry if I wasn't clear. Localhost isn't working with the new code, as I posted. Localhost does work with the previous version, where the response is "This page is under construction!". What I posted is basically where things broke down.

1 Answer

Don't forget that in this video we ran the command 'nodemon --debug-brk src/app' command and the 'node-inspector'.

This means that each time the server restarts, it will stop the app before it can actually do anything, and you'll have to hit the "play" button in the Node Inspector browser window.

You might want to stop that process and restart the server with the other command line, 'nodemon src/app'.