Monday 8 May 2017

Getting a 404 trying to http GET from my Node+Express API

I am trying to build a RESTful API and have hit a roadblock. I do not understand why I'm getting a 404 response.

I'm using HTTPie doing http GET localhost:3000/api/users/

And receiving (in the server's console):

Request URL: /api/users/ Request Type: GET hello from get, calling next [{"_id":"58e8200bda045823ad9e147d","created_at":"2017-04-07T23:26:03.354Z","name":"Matt","email":"xxxx@xxxx.com","gamesPlayed":100,"wins":86,"__v":0},{"_id":"58e82112eb158223dabf54ce","created_at":"2017-04-07T23:30:26.373Z","name":"Leif","email":"leify@blah.com","gamesPlayed":100,"wins":92,"__v":0},{"_id":"58eb74456f2d1c10e907702b","created_at":"2017-04-10T12:02:13.026Z","name":"Leif","email":"leify@blah.com","gamesPlayed":100,"wins":92,"__v":0},{"_id":"58eb838d3b521817fa507b0e","created_at":"2017-04-10T13:07:25.668Z","name":"Leif","email":"leify@blah.com","gamesPlayed":100,"wins":92,"__v":0},{"_id":"58eb84370666cb185d78b5af","created_at":"2017-04-10T13:10:15.122Z","name":"Leif","email":"leify@blah.com","gamesPlayed":100,"wins":92,"__v":0},{"_id":"58eb84eeef35b5189b417187","created_at":"2017-04-10T13:13:18.376Z","name":"Leif","email":"leify@blah.com","gamesPlayed":100,"wins":92,"__v":0}] GET /api/users/ 404 41.989 ms - 149

That data is being console.log'd in the middleware. It's logging res.json

And response from HTTPie:

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 149
Content-Security-Policy: default-src 'self'
Content-Type: text/html; charset=utf-8
Date: Mon, 08 May 2017 13:52:40 GMT
X-Content-Type-Options: nosniff
X-Powered-By: Express

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /api/users/</pre>
</body>
</html>

Content-Type should not be HTML, it should be JSON. According to express docs, using res.json() is the correct way to set that, which I believe I am doing.

Here is SOME code, not all, for simplicity reasons. I can show it all if need be but I think these are the important bits.

server.js

var api = require('./api/api')
var app = express()
app.use('/api', api)

api.js

var router = require('express').Router()
var users = require('./users/usersRoutes')
router.use('/users', users)
module.exports = router

userRoutes.js

var router = require('express').Router()
var logger = require('../../utils/logger')
var controller = require('./usersController')

// if id was passed, run params
router.param('id', controller.params)

// root
router.route('/')
  .get(controller.get, function(req,res,next) {
    logger.log(res.json)
    next()
  })

get function of usersController.js

get: function(req, res, next) {
    var promise = User.find().exec()
    promise.then(function(users){
      if (!users) {
        next(new Error('Didn\'t find any users'))
      } else {
        logger.log('hello from get, calling next')
        res.json = users // attach response
        next() // call next, could be CRUD op
      }
    })
    .catch(function(err) {
      next(err) // pass error up
    })
  },

Any help is greatly appreciated, thank you.



via kaleoh

No comments:

Post a Comment