@clysema/http v0.4.2
@clysema/http
Serves or HTTP static content and/or a REST GET/POST endpoints. Can be used to create a local user interface. Basic Auth.
Install
$ npm install @clysema/httpUsage
config/http.json
{
"root": "/home/pi/www",
"host": "localhost",
"port": 4000,
"rest": true,
"get": ["vars"],
"post": ["controls"]
}If root property exis, will serve static content placed in /home/pi/www on http://localhost:4000.
If rest is true and get list is defined, a GET in http://localhost:4000/vars endpoint will return app.vars in this example.
If rest is true and post list is defined, a POST in http://localhost:4000/controls will write app.controls (in this example) with the data supplied.
Anywhere in the app code:
// (simply setup app.vars object) for the get endpoint
app.vars = { date: new Date() };
// log the controls
console.log(app.controls);Test using cURL
GET
curl http://localhost:4000/varsPOST
curl --header "Content-Type: application/json" --request POST --data '{"a":"1","b":"2"}' http://localhost:4000/controlsSecurity
Only allow local requests (recommended):
config/http.json
{
"host": "localhost"
}Allow requests from anywhere:
config/http.json
{
"host": "0.0.0.0"
}Basic Auth
Put the username and password in env variables and the run the controller app.
Example:
USERNAME=test PASSWORD=test npm startTest
GET/controls
curl -u test:test http://localhost:4000/controlsPUT/controls
curl -d '{"a":3, "b":3}' -u test:test -H "Content-Type: application/json" -X POST http://localhost:4000/controls