简体   繁体   中英

How to connect my mobile app (written in lua) and my server (written in node.js)?

I am new to lua and node js and I am trying to connect the mobile app that I'm working on to the server. The problem is it connects to the server but the data that I am trying to pass gets lost or it doesn't reach the server. Any ideas on what's wrong with what I'm doing?

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');
 });

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

Your code is Ok, except that it seems your network listener networkListener() is declared after your store() function. Lua cannot access stuff declared after what you are executing, unless it is forward declared. So, lua does not find the listener and it does not get called, even if there was an error. This function should be declared before your store() function, so that it can access it, like this:

local function networkListener(event)
    ...
end

local function store()
    ...
end

That, or you could forward declare it, sort of like this:

local networkListener = nil -- This forward declaration

local function store()
   ...
end

networkListener = function()
    ...
end

Here is more info about lua forward declaration . I know this is the case since you provided us with a screenshot of your actual code order. You can always use a debugger to see if everything is working OK after you try the solution. I recommend the IDE zerobrane studio .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM