2016-07-10 13:02:30 +00:00
|
|
|

|
2016-07-07 19:07:26 +00:00
|
|
|
|
2017-06-23 08:21:56 +00:00
|
|
|
A simple vanilla JavaScript router a la [ExpressJS](http://expressjs.com/).
|
|
|
|
|
|
|
|
Code the front-end like the back-end.
|
2017-06-22 19:17:18 +00:00
|
|
|
|
2017-06-22 19:22:12 +00:00
|
|
|
[frontexpress demo](https://github.com/camelaissani/frontexpress-demo)
|
2017-06-22 19:17:18 +00:00
|
|
|
|
2016-06-26 15:28:08 +00:00
|
|
|
[](https://travis-ci.org/camelaissani/frontexpress)
|
2016-07-14 21:09:13 +00:00
|
|
|
[](https://codeclimate.com/github/camelaissani/frontexpress)
|
2016-06-26 15:28:08 +00:00
|
|
|
[](https://coveralls.io/github/camelaissani/frontexpress?branch=master)
|
2017-01-13 14:18:54 +00:00
|
|
|

|
2017-06-22 19:17:18 +00:00
|
|
|

|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2016-09-08 18:28:54 +00:00
|
|
|
### From npm repository
|
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
```bash
|
|
|
|
$ npm install frontexpress
|
|
|
|
```
|
|
|
|
|
2016-09-08 18:28:54 +00:00
|
|
|
### From bower repository
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ bower install frontexpress
|
|
|
|
```
|
|
|
|
|
2016-09-13 22:20:30 +00:00
|
|
|
### From CDN
|
|
|
|
|
2017-06-22 19:22:12 +00:00
|
|
|
On [jsDelivr](https://cdn.jsdelivr.net/npm/frontexpress@1.2.0/frontexpress.min.js)
|
2016-09-13 22:20:30 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
## Usage
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
```js
|
|
|
|
import frontexpress from 'frontexpress';
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
// Front-end application
|
|
|
|
const app = frontexpress();
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
// front-end logic on navigation path "/page1"
|
|
|
|
app.get('/page1', (req, res) => {
|
|
|
|
document.querySelector('.content').innerHTML = res.responseText;
|
|
|
|
});
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
// front-end logic on navigation path "/page2"
|
|
|
|
app.get('/page2', (req, res) => {
|
|
|
|
document.querySelector('.content').innerHTML = res.responseText;
|
|
|
|
});
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
// start front-end application
|
|
|
|
app.listen();
|
2016-07-07 19:07:26 +00:00
|
|
|
```
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
### Routes
|
2016-07-23 09:46:03 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Listen GET requests on path /hello:
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
```js
|
2016-07-26 19:05:27 +00:00
|
|
|
app.get('/hello', (req, res) => {
|
2016-07-09 22:58:03 +00:00
|
|
|
window.alert('Hello World');
|
2016-07-07 19:07:26 +00:00
|
|
|
});
|
|
|
|
```
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Listen POST requests on path /item:
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
```js
|
2016-07-26 19:05:27 +00:00
|
|
|
app.post('/item', (req, res) => {
|
|
|
|
window.alert('Got a POST request at /item');
|
2016-07-07 19:07:26 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Listen GET requests on path starting with /api/:
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
```js
|
2016-07-09 22:58:03 +00:00
|
|
|
app.get(/^api\//, (req, res) => {
|
2016-07-07 19:07:26 +00:00
|
|
|
console.log(`api was requested ${req.uri}`);
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Get parameters from path
|
|
|
|
|
|
|
|
```js
|
|
|
|
app.get('/product/:id', (req, res) => {
|
|
|
|
// if we have /product/42 then
|
|
|
|
// req.params.id = 42
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
app.get('/user/:firstname?/:lastname', (req, res) => {
|
|
|
|
// if we have /user/camel/aissani then
|
|
|
|
// req.params.firstname = 'camel'
|
|
|
|
// req.params.lastname = 'aissani'
|
|
|
|
|
|
|
|
// if we have /user/aissani then
|
|
|
|
// req.params.firstname = undefined
|
|
|
|
// req.params.lastname = 'aissani'
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
|
|
|
app.get('/user/:id', (req, res) => {
|
|
|
|
// if we have /user/1,2,3 then
|
|
|
|
// req.params.id = [1,2,3]
|
|
|
|
});
|
|
|
|
```
|
|
|
|
### Middleware object
|
|
|
|
|
|
|
|
The middleware object gives access to more hooks
|
|
|
|
|
|
|
|
```js
|
|
|
|
class MyMiddleware = new Middleware {
|
|
|
|
entered(req) {
|
|
|
|
// before request sent
|
|
|
|
}
|
|
|
|
|
|
|
|
updated(req, res) {
|
|
|
|
// after request sent
|
|
|
|
// res has the request response
|
|
|
|
window.alert('Hello World');
|
|
|
|
}
|
|
|
|
|
|
|
|
exited(req) {
|
|
|
|
// before a new request sent
|
|
|
|
}
|
|
|
|
|
|
|
|
failed(req, res) {
|
|
|
|
// on request failed
|
|
|
|
}
|
|
|
|
|
|
|
|
next() {
|
|
|
|
// for chaining
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
app.get('/hello', new MyMiddleware());
|
|
|
|
```
|
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
### Chain handlers
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
You can provide multiple handlers functions on a navigation path. Invoking ```next()``` function allows to chain the handlers.
|
|
|
|
At the opposite, when the ```next()``` method is not called the handler chain is stopped.
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
const h1 = (req, res, next) => { console.log('h1!'); next(); };
|
|
|
|
const h2 = (req, res, next) => { console.log('h2!') };
|
|
|
|
const h3 = (req, res, next) => { console.log('h3!'); next(); };
|
|
|
|
|
|
|
|
app.get('/example/a', h1);
|
|
|
|
app.get('/example/a', h2);
|
|
|
|
app.get('/example/a', h3);
|
|
|
|
```
|
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
On navigation on path /example/a, the browser console displays the following:
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
h1!
|
|
|
|
h2!
|
|
|
|
```
|
|
|
|
|
|
|
|
h3 is ignored because ```next()``` function was not invoked.
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
#### app.route()
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
You can create chainable route handlers for a route path by using ```app.route()```.
|
|
|
|
|
|
|
|
```js
|
|
|
|
app.route('/book')
|
2016-07-09 22:58:03 +00:00
|
|
|
.get((req, res) => { console.log('Get a random book') })
|
|
|
|
.post((req, res) => { console.log('Add a book') })
|
|
|
|
.put((req, res) => { console.log('Update the book') });
|
2016-07-07 19:07:26 +00:00
|
|
|
```
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
#### frontexpress.Router
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
Use the ```frontexpress.Router``` class to create modular, mountable route handlers.
|
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
Create a router file named ```birds.js```:
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
import frontexpress from 'frontexpress';
|
|
|
|
|
2016-07-09 22:58:03 +00:00
|
|
|
const router = frontexpress.Router();
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
// specific middleware for this router
|
2016-07-07 19:07:26 +00:00
|
|
|
router.use((req, res, next) => {
|
2016-07-09 22:58:03 +00:00
|
|
|
console.log(`Time: ${Date.now()}`);
|
|
|
|
next();
|
|
|
|
});
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
// listen navigation on the home page
|
2016-07-09 22:58:03 +00:00
|
|
|
router.get('/', (req, res) => {
|
2016-07-07 19:07:26 +00:00
|
|
|
document.querySelector('.content').innerHTML = '<p>Birds home page</p>';
|
2016-07-09 22:58:03 +00:00
|
|
|
});
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2016-07-26 19:05:27 +00:00
|
|
|
// listen navigation on the about page
|
2016-07-09 22:58:03 +00:00
|
|
|
router.get('/about', (req, res) => {
|
|
|
|
document.querySelector('.content').innerHTML = '<p>About birds</p>';
|
|
|
|
});
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
export default router;
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, load the router module in the app:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import birds from './birds';
|
|
|
|
...
|
|
|
|
app.use('/birds', birds);
|
|
|
|
```
|
|
|
|
|
2017-06-22 19:22:12 +00:00
|
|
|
## More
|
|
|
|
|
|
|
|
[API](https://github.com/camelaissani/frontexpress/blob/master/docs/api.md)
|
2016-07-23 09:35:39 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
## Tests
|
2016-07-23 08:57:41 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Clone the git repository:
|
2016-07-23 08:57:41 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
```bash
|
|
|
|
$ git clone git@github.com:camelaissani/frontexpress.git
|
|
|
|
$ cd frontexpress
|
2016-07-23 09:35:39 +00:00
|
|
|
```
|
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
Install the dependencies and run the test suite:
|
2016-07-23 08:57:41 +00:00
|
|
|
|
2017-06-22 19:17:18 +00:00
|
|
|
```bash
|
|
|
|
$ npm install
|
|
|
|
$ npm test
|
2016-07-23 09:35:39 +00:00
|
|
|
```
|
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
## License
|
|
|
|
|
2017-06-22 19:22:12 +00:00
|
|
|
[MIT](LICENSE)
|