https://github.com/t-vk/nodemcu-express
HTTP Web Server library for ESP8266 modules that run the NodeMCU firmware. (Acts like express.js from node.js, but with Lua instead of JavaScript.)
https://github.com/t-vk/nodemcu-express
esp8266 express expressjs http-server lua nodemcu web-server
Last synced: 10 months ago
JSON representation
HTTP Web Server library for ESP8266 modules that run the NodeMCU firmware. (Acts like express.js from node.js, but with Lua instead of JavaScript.)
- Host: GitHub
- URL: https://github.com/t-vk/nodemcu-express
- Owner: T-vK
- License: mit
- Created: 2017-04-30T00:33:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-20T08:11:10.000Z (almost 7 years ago)
- Last Synced: 2025-04-11T06:14:38.912Z (about 1 year ago)
- Topics: esp8266, express, expressjs, http-server, lua, nodemcu, web-server
- Language: Lua
- Homepage:
- Size: 17.6 KB
- Stars: 19
- Watchers: 3
- Forks: 10
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Express.js-like HTTP server library for NodeMCU
## About
This HTTP server library is very similar to the popular node.js module [express.js](https://expressjs.com/en/starter/hello-world.html).
It's extremely intuitive and easy to use thus and I'm making the interface as similar as possible.
The library is written in Lua.
## Features (current and upcoming)
[Go to the project site.](https://github.com/T-vK/NodeMCU-Express/projects/1)
## Serving files
Serving files is really easy and can be done with a single line of code.
Example of serving the file `home.html` at url `/home`:
``` Lua
app:use('/home',express.static('home.html'))
```
To serve all files that are in a certain directory, let's say the directory is called `foo`, you can do:
``` Lua
app:use('/example',express.static('foo'))
```
If you have a file called `foo/test.html` on your ESP8266 module and it's IP is 192.168.1.10, then you will be able to access it by visiting `http://192.168.1.10/example/text.html`.
## Routes
A route consists of a URL path and a function. Whenever someone visits the URL path, the function gets called.
For instance:
``` Lua
-- Create a new route at `/led-on` an the connected function turns an LED on
app:get('/led-on',function(req,res)
gpio.write(4, gpio.HIGH) -- set GPIO 2 to HIGH
res:send('Led turned on!')
end)
```
We always start our path with `/`. If your ESP8266 has the IP 192.168.1.111 then you can open the route `/led-on` by typing `http://192.168.1.111/led-on` into your browser.
## Middlewares
A middleware consists of a function that gets called every time someone make an http request to our ESP8266 module.
Using a middleware we can easily extend the functionality of our http server.
For instance to add a cookie parser, a request logger, an authentication mechanism and muuuuuch more.
Here is an example for a request logger:
``` Lua
app:use(function(req,res,next)
print('New request!' .. ' Method: ' .. req.method .. ' URL: ' .. req.url)
next()
end)
```
## Example
For a full example go here: [example.lua](example.lua)
Here is the short version:
``` Lua
require('HttpServer')
local app = express.new()
app:listen()
-- Define a new middleware that prints the url of every request
app:use(function(req,res,next)
print(req.url)
next()
end)
-- Define a new route that just returns an html site that says "HELLO WORLD!"
app:get('/helloworld',function(req,res)
res:send('HELLO WORLD!')
end)
-- Serve the file `home.html` when visiting `/home`
app:use('/home',express.static('home.html'))
-- Serve all files that are in the folder `http` at url `/libs/...`
-- (To be more accurate I'm talking about all files starting with `http/`.)
app:use('/libs',express.static('http'))
```
Of course your ESP8266 needs to be connected to your WiFi or your ESP8266 has to host an AP that you are connecting to.
## How to use it
Just upload `HttpServer.lua` to your ESP8266, then you can use `require('HttpServer') just like in my example in your `init.lua`.
## Need help? Have a feature request? Found a bug?
Create an issue right here on github.