【问题标题】:POST request don't work in restify, getting a 500 internal server errorPOST 请求在 restify 中不起作用,出现 500 内部服务器错误
【发布时间】:2014-09-06 15:48:27
【问题描述】:

我对示例 REST API 中的 POST 请求有一点问题。

我只有四个路由的四个回调方法:

app.get '/tasks', task.getAll
app.get '/tasks/done', task.getDone
app.get '/tasks/notdone', task.withoutDone
app.post '/tasks', task.newTask

和:

models = 需要'../models/models.js'

exports.getAll = (req, res, next) ->
  models.Task.find {}, 'title description, done', (err, tasks) ->
    if err then err
    res.send tasks
    do next

exports.getDone = (req, res, next) ->
  models.Task.find {done: true}, (err, tasks) ->
    if err then err
    res.send tasks
    do next

exports.withoutDone = (req, res, next) ->
  models.Task.find {done: false}, (err, tasks) ->
    if err then err
    res.send tasks
    do next

exports.newTask = (req, res, next) ->
  if req.params.title is undefined
    return next new restify.InvalidArgumentError 'Title is needed'

  taskData = 
    title: req.params.title
    description: req.params.description
    done: req.params.done

  task = new Task(taskData)
  task.save (err, data) ->
    if err
      return next new restify.InvalidArgumentError(JSON.stringify(error.errors))
    else
      res.json(data)

    res.send 201, task

因此,您可以在一个独特的要点中找到代码here

我运行时的痕迹

curl -i -X POST -d '{"title": "hello world", description: "lorem ipsum dolor amet", "done": true}' http://localhost:8080/tasks

在响应中返回给我一个 500 内部负责人:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Content-Length: 59
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, Api-Version, Response-Time
Access-Control-Allow-Methods: GET, POST
Access-Control-Expose-Headers: Api-Version, Request-Id, Response-Time
Connection: Keep-Alive
Content-MD5: QFnWTtR6KfhLtGqWpGWZog==
Date: Mon, 17 Mar 2014 15:12:00 GMT
Server: task-API
Request-Id: 7d71a510-ade6-11e3-bd80-292785b198e2
Response-Time: 1

{"code":"InternalError","message":"restify is not defined"}

【问题讨论】:

  • 你能分享更多你的服务器js文件吗?错误消息“未定义 restify”听起来像是您没有包含 restify 或使用不同的变量名。

标签: node.js rest restify


【解决方案1】:

Restify 在错误处理方面与 expressjs 的设计相似。它吞下未捕获的异常并将它们以 JSON 格式发送回浏览器,并伴有 HTTP 500 错误,而不是在控制台打印它们。

“restify 未定义”消息通常表示您忘记在脚本中的某处调用 var restify = require('restify');

您也可以使用以下sn-p在控制台查明错误的位置:

server.on('uncaughtException', function (req, res, route, err) {
    console.log('uncaughtException', err.stack);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多