Testing REST routes with curl --data, returns 404

2020-04-19 05:34发布

I am attempting a MEAN stack tutorial and I am at the "Testing the Initial Routes" step, where we test our REST routes using cURL.

I am trying to run this command to create a new post:

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

However, I keep getting errors. At first the error was:

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.

Then after some SO purusing, I found that I needed to use double quotes since I am on windows. However, I'm still getting an error:

<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>

I tried running the next command in the tutorial:

curl http://localhost:3000/posts

This resulted in the exact same error. Then I checked the url in my browser and it is actually: http://localhost:3000/#/posts. So I tried running the above command but with the # url and it returned the html for that page. So then I tried running the initial command with the # url but I got the same error.

Not sure what to try next. I followed the tutorial exactly and even went back and copy/pasted code to make sure I had it exactly correct but I'm still getting an error.

3条回答
虎瘦雄心在
2楼-- · 2020-04-19 05:34

You need to define mongoose before express. Easiest to just define mongoose at the very top of app.js:

var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');

mongoose.connect('mongodb://localhost/news');

var express = require('express');
var path = require('path');

...
查看更多
孤傲高冷的网名
3楼-- · 2020-04-19 05:53

So it turns out it my mongoose schema was defined below module.exports = app; in app.js. So I just had to move those definitions above and it worked out. So to whoever submitted suggested edits to me remove the mongoose tag from this post, thanks a lot. NOT.

查看更多
老娘就宠你
4楼-- · 2020-04-19 06:00

I am not sure what flavor of a shell you are using but most use # to designate the following characters are a comment. So you would need to quote the actual URL as well. Also, the link might need to be url-encoded. You can also use -d as shorthand for the data values.

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

查看更多
登录 后发表回答