如何连接我的移动应用程序(用lua编写)和我的服务器(用node.js编写)?

亚娜·丹尼斯(YannaH Denise)

我是lua和node js的新手,我正在尝试将正在开发的移动应用程序连接到服务器。问题是它连接到服务器,但是我尝试传递的数据丢失或没有到达服务器。关于我在做什么的任何想法吗?

这是我连接到服务器的lua代码

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

`

这是我尝试向其发送请求的简单服务器的代码

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

////////////////////////////////////////////////////////////////////////////////
大教堂德国人

您的代码还可以,只是看起来您的网络监听器networkListener()store()函数之后声明Lua无法访问正在执行的内容之后声明的内容,除非它被预先声明。因此,即使有错误,lua也不会找到侦听器,也不会被调用。应该在store()函数之前声明此函数,以便可以访问它,如下所示:

local function networkListener(event)
    ...
end

local function store()
    ...
end

那样,或者您可以向前声明它,就像这样:

local networkListener = nil -- This forward declaration

local function store()
   ...
end

networkListener = function()
    ...
end

这是有关lua forward声明的更多信息我知道是这种情况,因为您向我们提供了您的实际代码顺序的屏幕截图。尝试解决方案后,您始终可以使用调试器查看是否一切正常。我建议使用IDE zerobrane工作室

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何部署单页应用程序。用ClojureScript / Figwheel编写的静态服务器?

用 Electron/Node.js 编写的打印服务器

在解析服务器应用程序上使用Node.js编写服务器端代码

用C ++编写类似于FTP的简单客户端/服务器应用程序

如何编写我的Web应用程序的代码,以便它知道它在使用什么Web服务器?

如何将控制器 (MVC) 添加到我用 Java 编写的 JavaFX 应用程序中?

如何将消息从用 Node.js 编写的本机应用程序发送到 Chrome 扩展程序?

我需要用node.js编写的服务器,而不是常规的nginx / Apache吗?

如何为用svelte.js编写的应用程序创建插件?

什么是CommonJS,我为什么要关心它如何帮助编写.js应用程序?

我有一个用Objective-c编写的iOS应用程序,还有一个带有WooCommerce的Wordpress网站。如何连接它们?

rm -rf 用 Node.js 编写

用Node.js编写JSON

如何将android客户端连接到用C编写的服务器?

用Kotlin编写javascript应用程序

用Swift编写Windows应用程序

用javascript编写桌面应用程序

用C编写应用程序

如何使我的服务器上的文件可用于客户端网络浏览器,用 Java 编写的服务器

如何编写服务器/客户端视频和音频流应用程序的代码?

如何安装用Python编写的应用程序?

如何知道用哪种语言编写的应用程序

我如何能完全用JavaScript编写iPhone应用程序而又不使其仅成为Web应用程序?

如何使用无服务器框架通过AWS API网关返回用Node.js编写的AWS Lambda函数的错误?

我该如何编写Clojure REPL应用程序?

如何为我已经用Java编写的程序制作GUI?

如果我已从Expo弹出(分离)应用程序,我可以再次用Javascript编写代码吗?

如何编写Node.js Express服务器以接收curl POST

用LUA脚本编写多个JS文件?(同时)