2016-10-06 5 views
2

私はluaとnode jsを初めて使用しています。私が作業しているモバイルアプリケーションをサーバーに接続しようとしています。問題はそれがサーバーに接続していますが、私が通過しようとしているデータが失われたり、サーバーに届かないということです。 私がやっていることに何が間違っているかについてのアイデアはありますか?私のモバイルアプリケーション(luaで書かれた)と私のサーバ(node.jsで書かれた)を接続するには?

this is my lua code to connect to the server

local function store() 

    local headers = {} 

    headers["Content-Type"] = "application/x-www-form-urlencoded" 
    headers["Accept-Language"] = "en-US" 

    local body = "fname="..fname 
    local params = {} 

    params.headers = headers 
    params.body = body 

    print(body) 
    print(headers) 
    print(params.body) 
    print(params.headers) 

    network.request("http://192.168.56.2:8888", "POST", networkListener, params) 

end 



local function networkListener(event) 
     if (event.isError) then 
     print("Network error!") 
    else 
     print ("RESPONSE: " .. event.response) 
     local serializedString = json.decode(event.response) 

          print(serializedString) 

     --data = json.decode(serializedString) 
     --print(serializedString.student[1]) 

    end 
end 

`

This is the code for the simple server I am trying to send a request to

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

var morgan = require('morgan'); 
var consolidate = require('consolidate'); 
var bodyparser = require('body-parser'); 
var parser = require('luaparse'); 

//////////////////////////////////////////////////////////////////////////////// 

app.listen(8888,function() {console.log('Server Running!');}); 

//////////////////////////////////////////////////////////////////////////////// 

app.set('views', __dirname + '/views'); 
app.engine('html', consolidate.nunjucks); 
app.use(morgan('dev')); 
app.use(bodyparser.urlencoded({ extended: true })); 
app.use('/static', express.static(__dirname + '/static')); 

//////////////////////////////////////////////////////////////////////////////// 

app.get('/', function(req, res) { 
    res.render('index.html'); 
}); 

app.post('/', function(req, res) { 
    var fname = req.fname; 
    var lname = req.body.lastname; 

    console.log("it went in"); 
    console.log(req.body.fname); 
    console.log(req.body); 
    console.log(req.header); 
    console.log("nahuman"); 


    res.render('index.html'); 
}); 

//////////////////////////////////////////////////////////////////////////////// 
+1

可能であれば、画像リンクの代わりに質問にコードを含めてください。 – vivek

+0

私の質問を編集し、コードの一部を含める –

答えて

1

あなたのコードは、あなたのネットワーク・リスナーnetworkListener()後のあなたのstore()機能宣言されていると思われることを除いて、OKです。あなたが実行しているものがの後にと宣言されたものにアクセスすることはできません。が宣言されている場合を除きます。したがって、luaはリスナーを見つけられず、エラーがあっても呼び出されません。それはそれにアクセスできるように、この関数は次のように、あなたのstore()関数の前に宣言する必要があります。

local function networkListener(event) 
    ... 
end 

local function store() 
    ... 
end 

それか、並べ替えのこのように、それを宣言転送することができます:

local networkListener = nil -- This forward declaration 

local function store() 
    ... 
end 

networkListener = function() 
    ... 
end 

Here is more info about lua forward declaration。実際のコードオーダーのスクリーンショットを提供して以来、これが当てはまることはわかっています。ソリューションを試した後、いつでもデバッガを使用して、すべてが正常に機能しているかどうかを確認できます。私はIDE zerobrane studioをお勧めします。

+0

ありがとう! :) –

関連する問題