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 Build a REST API With Express Building API Routes in Express Building the Answer Routes

Marcos Cabrini Riani dos Reis
Marcos Cabrini Riani dos Reis
12,253 Points

ERROR 404 POST /questions/6/anwers 404 8.992 ms - 32

Error in the console

qa-rest-api(master) > node app.js Express server is listening on port 3000 POST /questions/6/anwers 404 8.992 ms - 32

My code Routes.js

'use strict';

var express = require('express');
var router = express.Router();

// GET / questions
// Route for questions collection
router.get('/', function (req, res) {
  res.json({ response: 'You sent me a Get request' });
});

// POST / questions
// Route for creating questions
router.post('/', function (req, res) {
  res.json({
    response: 'You sent me a Post request',
    body: req.body,
  });
});

// GET / questions/:qID
// Route for specific questions
router.get('/:qID', function (req, res) {
  res.json({
    response: 'You sent me a Get request for ID' + req.params.qID,
  });
});

// POST / questions/:qID/answers
// Route for creating an answer
router.post('/:qID/answers', function (req, res) {
  res.json({
    response: 'You sent me a Post request to /answers',
    questionId: req.params.qID,
    body: req.body,
  });
});

// PUT / questions/:qID/answers/:aID
// Edit a specific answer
router.put('/:qID/answers/:aID', function (req, res) {
  res.json({
    response: 'You sent me a Put request to /answers',
    questionId: req.params.qID,
    answerId: req.params.aID,
    body: req.body,
  });
});

// DELETE / questions/:qID/answers/:aID
// Elete a specific answer
router.delete('/:qID/answers/:aID', function (req, res) {
  res.json({
    response: 'You sent me a Delete request to /answers',
    questionId: req.params.qID,
    answerId: req.params.aID,
  });
});

// POST / questions/:qID/answers/:aID/vote-up
// POST / questions/:qID/answers/:aID/vote-down
// Vote a specific answer
router.post('/:qID/answers/:aID/vote-:dir', function (req, res) {
  res.json({
    response: 'You sent me a POST request to /vote-' + req.params.dir,
    questionId: req.params.qID,
    answerId: req.params.aID,
    vote: req.params.dir,
  });
});

module.exports = router;

Code App.js

'use strict';

var express = require('express');
var app = express();
var routes = require('./routes');

var jsonParser = require('body-parser').json;
var logger = require('morgan');

app.use(logger('dev'));
app.use(jsonParser());

app.use('/questions', routes);

var port = process.env.PORT || 3000;

app.listen(port, function () {
  console.log('Express server is listening on port', port);
});