nodemcu php服务器,NodeMCU作为网页服务器

借鉴 这篇文章 做了一个简单的服务程序。

程序总共5个文件:httpServer.lua,index.html,init.lua,spectre.css,zepto.js。

httpServer.lua是使用的这里 的HTTP服务库,界面程序使用了轻量的Zepto.js与Spectre.css来搭建前端页面。其中Spectre.css删掉了大部分没有用到的功能。

首先是init文件的编写:

建立一个WiFi并打开HTTP服务

wifi.setmode(wifi.SOFTAP)

cfg = {}

cfg.ssid = "1004"

cfg.pwd = "10041004"

wifi.ap.config(cfg)

print(wifi.ap.getip())

-- Serving static files

dofile('httpServer.lua')

httpServer:listen(80)

处理对应的请求:

httpServer:use('/log', function(req, res)

res:sendFile('1004.log')

end)

-- 返回记录的登陆log信息

httpServer:use('/login', function(req, res)

if req.query.name ~= nil and req.query.email ~= nil then

print('Hello: ' .. req.query.name.. req.query.email)

if file.open("1004.log", "a+") then

file.write('Name : '..req.query.name..'\n')

file.write('Email: '..req.query.email..'\n')

file.write('---\n')

file.close()

end

--将提交的表单写入log保存

print('LOG OK')

else

res:send('hello')

end

end)

访问根目录返回的index.html如下:

1004打卡

签到

名字

邮箱

提交

String.prototype.format = function() {

var args = arguments;

return this.replace(/\{(\d+)\}/g

, function(m,i) {

return args[i];

});

}

$(function() {

$('#connect').click(function() {

$.get('../login?name={0}&email={1}'.format($('#name').val(), $('#email').val()));

alert('签到成功!')

})

})

18740fe0773d

Screenshot_2017-05-15-18-18-55-260_com.android.br.png

18740fe0773d

Screenshot_2017-05-15-18-19-05-761_com.android.br.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值