Git Product home page Git Product logo

nodemcu-httpserver's Introduction

Express.js-like HTTP server library for NodeMCU

This library is a fork of the original library by T-vK: NodeMCU-Express. Improvements include;

  • Execute routes/middlewares in correct order
  • Corrected path matching for routes vs middlewares

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.

Get Started

Using this library is pretty simple.

  1. Download HttpServer.lua from this repository
  2. Upload HttpServer.lua to your device
  3. Require the library require('HttpServer') in your application code

Example

For a full example go here: example.lua

Here is the short version:

require('HttpServer')

local app = express.new()

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

app:listen()

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.

Note: regular expressions are currently not implemented.

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)

Need help? Have a feature request? Found a bug?

Create an issue right here on github.

nodemcu-httpserver's People

Contributors

sebtoombs avatar

Watchers

James Cloos avatar  avatar

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.