From 79ac822adba32f00c8bf44e967532566bad69d9d Mon Sep 17 00:00:00 2001 From: Camel Aissani Date: Sat, 23 Jul 2016 14:41:37 +0200 Subject: [PATCH] replaced main sample in README --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bcc33ab..ae8be55 100644 --- a/README.md +++ b/README.md @@ -7,24 +7,60 @@ [![Coverage Status](https://coveralls.io/repos/github/camelaissani/frontexpress/badge.svg?branch=master)](https://coveralls.io/github/camelaissani/frontexpress?branch=master) ![Coverage Status](https://david-dm.org/camelaissani/frontexpress.svg) +Let's assume having this html page: + +```html + + + + + + + +
+ + +``` + +Here the code managing routes front-end side: + ```js import frontexpress from 'frontexpress'; + +// Frontend application const app = frontexpress(); -// listen HTTP GET request on path (/hello) -app.get('/hello', (req, res) => { - document.querySelector('.content').innerHTML = '

Hello World

'; +// listen HTTP 401 error +// display an alert on 401 UNAUTHORIZED +app.use((req, res, next) => { + if (res.status === 401) { + window.alert('Restricted area!!!'); + } else { + next(); + } }); -// listen HTTP GET request on API (/api/xxx) -// update page content with response -app.get(/^\/api\//, (req, res, next) => { - document.querySelector('.content').innerHTML = res.responseText; - next(); +// listen HTTP GET requests from http://httpbin.org/get +// update html page with response content +app.get('http://httpbin.org/get', (req, res, next) => { + const obj = JSON.parse(res.responseText); + document.querySelector('.content').innerHTML = `

${obj.args.page}

`; + next(); }); // start listening frontend application requests -app.listen(); +app.listen(() => { + // Register website buttons + const buttons = document.querySelectorAll('button'); + for (let i=0; i { + // make an ajax call + app.httpGet(url); + }); + } +}); ``` ## Installation