2011-08-13 7 views
2

나는 명시 응용 프로그램과 POST 경로가 : 나는 ~ $ curl -X POST "http://xxxx.herokuapp.com/test?user=hello" 수행하려고Node.js : POST에서 매개 변수를 전달하는 방법은 무엇입니까?

app.post('/test', function(req, res){ 

//res.send(req.body.title + req.body.body) 
console.log(req.params); 
console.log(req.body); 
console.log(req.body.user); 
console.log(req.body.feedback); 
console.log("ok"); 
//return; 
}); 

를 내가 얻을 :

TypeError: Cannot read property 'user' of undefined 
    at Router.<anonymous> (/app/app.js:40:22) 
    at done (/app/node_modules/express/lib/router/index.js:250:22) 
    at middleware (/app/node_modules/express/lib/router/index.js:244:9) 
    at param (/app/node_modules/express/lib/router/index.js:227:11) 
    at pass (/app/node_modules/express/lib/router/index.js:232:6) 
    at Router._dispatch (/app/node_modules/express/lib/router/index.js:255:4) 
    at Object.handle (/app/node_modules/express/lib/router/index.js:45:10) 
    at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15) 
    at Object.methodOverride [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5) 
    at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15)~ $ 

안된다는 POST 작업을?

감사

답변

4

아니, 당신이 전달하는 매개 변수는 게시물 본문에 있지만 쿼리 문자열에 있지 않습니다. the docs에 따르면

, 당신이해야 할 액세스하기 : 당신이 URL에 인수를 추가되기 때문에 작동하지 않습니다

req.query.user 
+0

그것은 작동하지만 지금은 시간 초과 오류를 받고 있어요 서버에서 – donald

+3

서버가 응답하지 않고 단지 로깅하기 때문입니다. 'res.send ('hi'); 또는 어떤 것을 할 필요가 없습니까? –

1

아니가 (당신이 GET을 위해 무엇을 할 것이라고 이잖아). 게시물의 경우 :

curl -d "user=hello" http://xxxx.herokuapp.com/test 
1

아니요, 컬 호출이 잘못되었습니다. 시도하십시오

curl -X POST "http://xxxx.herokuapp.com/test" -d user=hello 
4

선택한 대답이 잘못되었습니다. 요청 문자열에서 POST 문자열이 아니라 요청 본문에서 무엇인가를 원한다는 것이 확실합니다. 당신은 확실히 몸 파서가 미들웨어 스택에 확인해야합니다 :이 작업을 수행 한 후에는 req.body을 사용할 수 있습니다

app.use(express.bodyParser()); 

.

(다른 언급 게다가, 당신의 컬뿐만 아니라 잘못된 것입니다. 당신은. 요청 본문 대신이 쿼리 문자열에 뭔가를 넣어 것)

+0

컬이 잘 작동하고 선택한 솔루션도 잘 작동합니다. – donald

+0

내가이 대답을 더 많이 할 수 있다면, 나는 그렇게 할 것이다. 이것은 내가 잃어 버렸던 바로 그 것이다. 고맙습니다. @chjj. – Crwth

관련 문제