【问题标题】:Getting a 404 trying to http GET from my Node+Express API尝试从我的 Node+Express API 获取 404 请求
【发布时间】:2017-10-06 14:26:04
【问题描述】:

我正在尝试构建 RESTful API,但遇到了障碍。我不明白为什么会收到 404 响应。

我正在使用 HTTPie 做 http GET localhost:3000/api/users/

并接收(在服务器的控制台中):

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

该数据在中间件中被控制台记录。它正在记录res.json

以及来自 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 不应该是 HTML,应该是 JSON。根据 express docs,使用 res.json() 是设置它的正确方法,我相信我正在这样做。

为了简单起见,这里有一些代码,不是全部。如果需要,我可以展示所有内容,但我认为这些是重要的部分。

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()
  })

获取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
    })
  },

非常感谢任何帮助,谢谢。

【问题讨论】:

  • res.json 是一个函数。你应该做res.json(users),而不是res.json = users

标签: node.js rest express


【解决方案1】:

在您的 usersController.js 控制器中:

logger.log('hello from get, calling next')

// res.json = users <=== WRONG
// res.json is a function

res.json(users)
next()

res.json 是一个函数,请在此处查看文档: https://expressjs.com/en/api.html#res.json

【讨论】:

    【解决方案2】:

    我认为代码有两个错误:

    首先,您没有结束响应,这就是您收到“404”响应的原因。

    而且,正如上面的回答所说,res.json 是 express() 中的一个响应函数,你不应该使用它来存储数据,使用另一个字段名称,例如 jsonData

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2016-09-13
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多