使用curl --data测试REST路由,返回404

时间:2015-04-10 02:14:54

标签: mongodb rest curl

我正在尝试MEAN堆栈教程,我正处于“Testing the Initial Routes”步骤,我们使用cURL测试REST路由。

我正在尝试运行此命令来创建新帖子:

curl --data 'title=test&link=http://test.com' http://localhost:3000/posts

然而,我一直在收到错误。起初错误是:

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
'link' is not recognized as an internal or external command,
operable program or batch file.

然后经过一些SO的推动,我发现我需要使用双引号,因为我在Windows上。但是,我仍然收到错误:

<h1>Not Found</h1>
<h2>404</h2>
<pre>Error: Not Found
at app.use.res.render.message (c:\base\demo\thinkster-mean-tutorial\flapper-news\app.js:30:13)
at Layer.handle [as handle_request] (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:302:13)
at c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:321:12)
at next (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:261:10)

at c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:603:15
at next (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:246:14)

at Function.proto.handle (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:166:3)
at router (c:\base\demo\thinkster-mean-tutorial\flapper-news\node_modules\express\lib\router\index.js:35:12)</pre>

我尝试在教程中运行下一个命令:

curl http://localhost:3000/posts

这导致完全相同的错误。然后我在浏览器中检查了网址,它实际上是:http://localhost:3000/#/posts。所以我尝试运行上面的命令,但使用#url并返回该页面的html。那么我尝试用#url运行初始命令,但是我得到了同样的错误。

不确定下一步该尝试什么。我完全按照教程,甚至回去复制/粘贴代码,以确保我完全正确,但我仍然收到错误。

3 个答案:

答案 0 :(得分:4)

你需要在表达之前定义mongoose。最容易在app.js的最顶部定义mongoose:

DeleteClient

答案 1 :(得分:2)

我不确定您使用的是什么样的shell,但大多数使用#来指定以下字符是注释。所以你需要引用实际的URL。此外,link可能需要进行网址编码。您还可以使用-d作为数据值的简写。

curl -d "title=test" --data-urlencode "link=http://test.com" "http://localhost:3000/#/posts"

答案 2 :(得分:0)

所以事实证明我的mongoose模式定义在module.exports = app;在app.js.所以我只需要将这些定义移到上面就可以了。所以对于提交建议编辑的人来说,从这篇帖子中删除了猫鼬标签,非常感谢。 NOT。