Git Product home page Git Product logo

nodemcu-express's Introduction

Express.js-like HTTP server library for NodeMCU

About

This HTTP server library is very similar to the popular node.js module express.js.
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.

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:

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:

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:

-- 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:

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

Here is the short version:

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('<html><head></head><body>HELLO WORLD!</body></html>')
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.

nodemcu-express's People

Contributors

t-vk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nodemcu-express's Issues

License?

Hi!

Nice little library you have here! I would like to use it, but the problem is that you do not have a software license attached to it.

Would you mind adding a license to it? It will be much easier for others to correctly use this software if it is correctly licensed. If you do not care how others will use it, and only want to protect yourself against lawsuits etc, I would recommend MIT or Apache 2.0 license for this project, as they are fairly open. If you want to restrict any projects using this library to open source, there are other licenses available.

bug in HttpServerLua

on line 173 there are this statement:
if body and this._headers['Content-Length'] == nil then
if i write this 2 line
app:use('/index.html')
app:use('/style.css')
the _headers['Content-Length'] value remain the length of first file
it is send with bad content-length value with undefined result
i replaced line 173 with:
if body then
and now work correctly

Example configuration not up to date

The WIFI part of the example does not match current nodemcu API.

use wifi.eventmon.register() instead of wifi.sta.eventMonReg
remove wifi.sta.eventMonStart() (which was outside the station mode block anyway)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.