首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POST请求在restify中不起作用,得到500个内部服务器错误

POST请求在restify中不起作用,得到500个内部服务器错误
EN

Stack Overflow用户
提问于 2014-03-17 18:53:14
回答 1查看 3.9K关注 0票数 1

我在一个示例REST中的POST请求有一个小问题。

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

代码语言:javascript
复制
app.get '/tasks', task.getAll
app.get '/tasks/done', task.getDone
app.get '/tasks/notdone', task.withoutDone
app.post '/tasks', task.newTask

以及:

模型=需要“./model/model s.js”

代码语言:javascript
复制
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

因此,您可以在唯一的gist中找到代码这里

当我奔跑时的痕迹

代码语言:javascript
复制
curl -i -X POST -d '{"title": "hello world", description: "lorem ipsum dolor amet", "done": true}' http://localhost:8080/tasks

返回是我的一个500内部的头在回应:

代码语言:javascript
复制
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"}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-28 09:48:32

在错误处理方面,Restify有类似的设计。它采用JSON格式,并伴有HTTP 500错误,吞食未察觉的异常并将它们发送回浏览器,而不是在控制台上打印它们。

消息"restify未定义“通常意味着您忘记在脚本中的某个地方调用var restify = require('restify');

您还可以使用以下代码段在控制台精确定位错误的位置:

代码语言:javascript
复制
server.on('uncaughtException', function (req, res, route, err) {
    console.log('uncaughtException', err.stack);
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22462895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档