2013-06-06 6 views
2

Node.js 및 Express에 큰 문제가 있습니다.Express bodyParser

내 요청의 본문 요소를 사용하고 싶지만 내 프로그램에서 bodyParser()을 사용하는 방법을 모른다.

requestServer = function(){ 
    var express = require('express'); 
    this.ex = express; 

    //this.app = require('express')(); 
    this.app = express(); 
    this.server = require('http').createServer(this.app); 
    this.io = require('socket.io').listen(this.server, {log: false}); 
    this.socket = []; 

    this.app.post('/test/', this.testFunction.bind(this)); 

    this.io.sockets.on('connection', this.socketConnection.bind(this)); 

    this.app.use(express.bodyParser()); 
}; 

... 

requestServer.prototype.positionChange = function(req, res){  
    console.log(req.body); // says its undefined??? 
    console.log(req.body.name); // also undefined :(
}; 

... 

var server = new requestServer(); 
server.listen(6667); 

을 내가 잘못 뭐하는 거지 : 그것은 자신을 위해보기 ... 단지 app.use()

아니다? 응용 프로그램이 반환되는 표현 객체이기 때문에

+1

어디서'positionChange'가 호출 되었습니까? – Prinzhorn

+1

'this'를 사용하는 이유가 무엇입니까? 조금 비 관습. –

+1

연결 청취를 시작하기 전에'app.use (express.bodyParser()); '를 이동하려 했습니까? – TheHippo

답변

0

var server = new requestServer(); 
server.listen(6667); 

var server = new requestServer(); 
server.app.listen(6667); 

수 없습니다. positionChange이 호출 된 전체 코드를 게시 할 수 있습니까?

1

nodejs를 사용하려면 express와 bodyParser를 사용하여 요청에 사용 된 메소드에 대해 명확히 설명하십시오. get 메소드 또는 게시물을 사용 하시겠습니까? 의사 소통을 통해 얻은 정보의 활용도에 따라 대답하십시오.

나는 당신이 body parser이 포스트 방법에 대해 생성 되었기 때문에 포스트 방법을 사용한다고 가정하고, 에 대한 query 방법을 얻을.

--- 클라이언트 측 : post 메소드를 사용하여 지정하는 양식 요소 만 작성하십시오. 각 입력 요소에 이름을 지정하십시오.

<form method="POST" action="/readRequest"> 
     <input type="text" name="email" hint="email" value="" size="40"/> 
     <inpnut type="submit" value="request" /> 
</form> 

--- 노드 측 : '.'당신의 /readRequest를 잡을으로 값을 읽을 수 미들웨어에 연산자가 요청 요소에서

function (req, res, next){ 
    console.log('Email received: '+req.body.email); 
    next(); 
} 
1

BodyParser는 더 이상 보안 악용으로 인해 널리 사용되지 않습니다. 이제 Express에 통합되었습니다. 사용하지 마십시오.

관련 문제