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 Building a MEAN Application Going MEAN with Express Setting Up an Express API

Sander de Wijs
PLUS
Sander de Wijs
Courses Plus Student 22,267 Points

[SOLVED] router not working correctly

I have followed along with the video and set up a router that uses the api/ namespace. However when I try to visit the /todos page, I get an error 'Cannot GET /todos'. This is my code:

    'use strict';

    var express = require('express');

    var app = express();

    app.use('/', express.static('public'));

    var router = express.Router();

    router.get('/todos', function(req, res){
        res.json({todos:[]});
    });

    app.use('/api', router);

    app.listen(3000, function() {
        console.log("Server running on port 3000");
    });

Hi Sander de Wijs, glad you sorted it out. I've marked your question as solved.

1 Answer

Sander de Wijs
PLUS
Sander de Wijs
Courses Plus Student 22,267 Points

I just discovered the answer. I thought by defining the route /api for the router I could just call localhost:3000/todos. But this isn't the case. I still need to call /api/todos as defined in the router.