From 2ce2dd6c9fe988cfa0980d42a15ae1346b58983f Mon Sep 17 00:00:00 2001 From: Harlan T Wood Date: Mon, 5 Dec 2016 23:48:09 -0800 Subject: [PATCH] Make examples easy to run: - generate bundle.js with browserify - add index.html which loads bundle.js - serve index and bundle via server - `npm run examples` generates bundle and starts server --- .gitignore | 3 +-- examples/client.js | 2 +- examples/index.html | 11 +++++++++++ examples/server.js | 15 +++++++++++++-- package.json | 5 ++++- 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 examples/index.html diff --git a/.gitignore b/.gitignore index acb3481..054f07d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ node_modules ./bitauth.js ./bitauth.min.js ./tests.js - - +/examples/bundle.js diff --git a/examples/client.js b/examples/client.js index cdeb258..667304d 100644 --- a/examples/client.js +++ b/examples/client.js @@ -53,6 +53,6 @@ for(k in keys) { } if(body) { console.log(body); - } + } }); } diff --git a/examples/index.html b/examples/index.html new file mode 100644 index 0000000..21286c2 --- /dev/null +++ b/examples/index.html @@ -0,0 +1,11 @@ + + + + + BitAuth Example + + +
Open the JS console, that's where the action is :)
+ + + diff --git a/examples/server.js b/examples/server.js index 490d5a4..ff6caba 100644 --- a/examples/server.js +++ b/examples/server.js @@ -2,6 +2,7 @@ var express = require('express'); var bodyParser = require('body-parser'); var rawBody = require('../lib/middleware/rawbody'); var bitauthMiddleware = require('../lib/middleware/bitauth'); +var path = require('path') var users = { 'Tf7UNQnxB8SccfoyZScQmb34V2GdEtQkzDz': {name: 'Alice'}, @@ -14,7 +15,6 @@ var app = express(); app.use(rawBody); app.use(bodyParser()); - app.get('/user', bitauthMiddleware, function(req, res) { if(!req.sin || !users[req.sin]) return res.send(401, {error: 'Unauthorized'}); res.send(200, users[req.sin]); @@ -32,4 +32,15 @@ app.get('/pizzas', function(req, res) { res.send(200, pizzas); }); -app.listen(3000); +app.get('/', function(req, res) { + res.sendFile(path.resolve(__dirname, 'index.html')) +}); + +app.get('/bundle.js', function(req, res) { + res.sendFile(path.resolve(__dirname, 'bundle.js')) +}); + +var port = 3000; +app.listen(port, function() { + console.log('Listening on http://localhost:' + port) +}); diff --git a/package.json b/package.json index cb9fca3..ca426b6 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,10 @@ ], "scripts": { "make-dist": "gulp browser", - "test": "gulp test" + "test": "gulp test", + "examples": "npm run examples:client:build && npm run examples:server", + "examples:client:build": "browserify examples/client.js -o examples/bundle.js", + "examples:server": "node examples/server.js" }, "main": "index.js", "version": "0.3.2",