2012-03-18 6 views
1

내가 애플릿을 가지고있다. (우리가 선택한 것이 아니라, MarioAI 엔진이다.) express를 사용하는 node.js 애플리케이션에 연결하고 싶다.하지만 mongodb가 값을 받아들이는 것처럼 보이지 않는다. 내 POST 요청을 localhost를 통해 보내고 있습니다. 노드에서 200 응답을 계속하지만 mongooose에서 '정의되지 않은', 내가 용의자가 Java에서 사용하는 URLEncoder 의미 문자열을 mangling 의미하는 어떻게 든 보내고있다. 애플릿에서 node.js에 POST하는 방법은 무엇입니까?

내가이 읽기 :

Problem with Java Applet to connect our server to call a PHP file

을 자바에서 다음의 OutputStreamWriter 통화 시도 : (Express 및 몽구스/MongoDB를 사용) 노드 응용 프로그램에서

//EvaluateFrustration() takes an int but should come back with a float value 
String frustrationString = Double.toString(EvaluateFrustration(this.periods)); 
try { 
    URL url = new URL("http://127.0.0.1:8888/mario");    
    final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setDoOutput(true); 
    conn.setRequestMethod("POST"); 
    System.out.println(conn.getResponseCode()); 
    conn.setUseCaches (false); 
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

    OutputStreamWriter writer; 


    writer = AccessController 
       .doPrivileged(new PrivilegedExceptionAction<OutputStreamWriter>() { 
        public OutputStreamWriter run() throws IOException { 
         return new OutputStreamWriter(conn.getOutputStream()); 
        } 
       }); 
     String data = URLEncoder.encode("frustrationValueFirstRound=" 
       + frustrationString,"UTF-8"); 
     writer.write(data); 
     writer.flush(); 

} catch (Exception e) { 
} 

을, 내가 쓴 :

var express = require('express'); 

var mongoose = require('mongoose'); 

var Schema = mongoose.Schema 
    , ObjectId = Schema.ObjectId; 

var ExperimentSchema = new Schema({ 
    experiment : ObjectId 
    , frustrationValueFirstRound : Number 
}); 

mongoose.connect('mongodb://localhost/mariopaper'); 
mongoose.model('Experiment', ExperimentSchema); 
var Experiment = mongoose.model('Experiment'); 

app.post('/mario', function(req, res){ 
    var exp = new Experiment(); 
    exp.frustrationValueFirstRound = req.body.frustrationValueFirstRound; 
    exp.save(function(err){ if (err) { throw err; } 
    res.send('ok'); 
}); 

참고로, 이 컬 전화에서 잘 작동합니다 :

curl -d "frustrationValueFirstRound=99" http://localhost:8888/mario 

누구나 단순히 자바에서 POST 잘못 작성했는지 어떤 아이디어를 가지고, 또는 아마도 내가 URLEncoder.encode()가 어떻게 작동하는지에 뭔가를 누락?

+0

다만주의 할을, 내가 전화하지 않고 시도했지만 나는 여러 게시물에 보았다 애플릿에서 자바 스크립트로 말하는 것은 권한 처리가 필요했습니다. 나는 항상 '정의되지 않음'을 얻지 만 모든 것을 시도하지 않았다. – pland

+0

'} catch (예외 e) { } '하지 마십시오. 적어도'e.printStackTrace() '를 호출하십시오. –

+0

저는 HTTP 연결을위한 Java API에 익숙하지 않습니다. 그렇지만 저는 여러분이라면 curl 호출과 Java 호출 사이에 Fiddler를 놓고 두 게시물을 비교할 것입니다. –

답변

0

나는 당신이 body-parser 노드 모듈을 가지고 있지 않기 때문에 "req.body 속성 아래에서 사용할 수있는 핸들러 앞에 미들웨어에 들어오는 요청 본문을 구문 분석하십시오."라고 생각합니다.

var express = require('express'); 

var mongoose = require('mongoose'); 

var bodyParser = require('body-parser'); 

app.use(bodyParser.json({limit: '50mb'})); 
app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); 

var Schema = mongoose.Schema 
    , ObjectId = Schema.ObjectId; 

var ExperimentSchema = new Schema({ 
    experiment : ObjectId 
    , frustrationValueFirstRound : Number 
}); 

mongoose.connect('mongodb://localhost/mariopaper'); 
mongoose.model('Experiment', ExperimentSchema); 
var Experiment = mongoose.model('Experiment'); 

app.post('/mario', function(req, res){ 
    console.log(req.body); // Is there something here ? 
    var exp = new Experiment(); 
    exp.frustrationValueFirstRound = req.body.frustrationValueFirstRound; 
    exp.save(function(err){ if (err) { throw err; } 
    res.send('ok'); 
}); 

소스 :

이 (몸 파서 모듈을 설치 한 후) 시도의 AccessController.doPrivileged를 통해서도 https://github.com/expressjs/body-parser

관련 문제