2016-07-10 13:02:30 +00:00
|
|
|
![frontexpress](http://fontmeme.com/embed.php?text=frontexpress&name=Atype%201%20Light.ttf&size=90&style_color=6F6F75)
|
2016-07-07 19:07:26 +00:00
|
|
|
|
2016-07-10 13:18:14 +00:00
|
|
|
Minimalist front end router framework à la [express](http://expressjs.com/)
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-06-26 15:28:08 +00:00
|
|
|
[![Build Status](https://travis-ci.org/camelaissani/frontexpress.svg?branch=master)](https://travis-ci.org/camelaissani/frontexpress)
|
2016-07-14 21:09:13 +00:00
|
|
|
[![Code Climate](https://codeclimate.com/github/camelaissani/frontexpress/badges/gpa.svg)](https://codeclimate.com/github/camelaissani/frontexpress)
|
2016-06-26 15:28:08 +00:00
|
|
|
[![Coverage Status](https://coveralls.io/repos/github/camelaissani/frontexpress/badge.svg?branch=master)](https://coveralls.io/github/camelaissani/frontexpress?branch=master)
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
import frontexpress from 'frontexpress';
|
|
|
|
const app = frontexpress();
|
|
|
|
|
|
|
|
// listen HTTP GET request on path (/)
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
window.alert('Hello World');
|
|
|
|
});
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-09 23:34:42 +00:00
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
|
2016-07-09 02:09:33 +00:00
|
|
|
// start listening frontend application requests
|
|
|
|
app.listen();
|
2016-07-07 19:07:26 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ npm install frontexpress
|
|
|
|
```
|
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
2016-07-09 23:37:38 +00:00
|
|
|
The quickest way to get started with frontexpress is to clone the [frontexpress-demo](https://github.com/camelaissani/frontexpress-demo) repository.
|
2016-07-07 19:07:26 +00:00
|
|
|
|
|
|
|
## Tests
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
Clone the git repository:
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
```bash
|
|
|
|
$ git clone git@github.com:camelaissani/frontexpress.git
|
|
|
|
$ cd frontexpress
|
|
|
|
```
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
Install the dependencies and run the test suite:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ npm install
|
|
|
|
$ npm test
|
|
|
|
```
|
|
|
|
|
2016-07-09 01:30:21 +00:00
|
|
|
## Disclaimer
|
|
|
|
|
|
|
|
>
|
|
|
|
> In this first version of frontexpress, the API is not completely the miror of the expressjs one.
|
|
|
|
>
|
|
|
|
> There are some missing methods. Currently, the use, get, post... methods having a middlewares array as parameter are not available.
|
|
|
|
> The string pattern to define route paths is not yet implemented.
|
|
|
|
>
|
|
|
|
> Obviously, the objective is to have the same API as expressjs when the methods make sense browser side.
|
|
|
|
>
|
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
## Routing
|
|
|
|
|
|
|
|
### Basic routing
|
|
|
|
|
|
|
|
Routing allows to link the frontend application with HTTP requests to a particular URI (or path).
|
|
|
|
The link can be specific to an HTTP request method (GET, POST, and so on).
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
The following examples illustrate how to define simple routes.
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
Listen an HTTP GET request on URI (/):
|
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('/', (req, res) => {
|
|
|
|
window.alert('Hello World');
|
2016-07-07 19:07:26 +00:00
|
|
|
});
|
|
|
|
```
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
Listen an HTTP POST request on URI (/):
|
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.post('/', (req, res) => {
|
|
|
|
window.alert('Got a POST request at /');
|
2016-07-07 19:07:26 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Route paths
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
Route paths, in combination with a request method, define the endpoints at which requests can be made.
|
|
|
|
Route paths can be strings (see basic routing section), or regular expressions.
|
2016-07-09 22:58:03 +00:00
|
|
|
|
2016-07-07 19:07:26 +00:00
|
|
|
This route path matches all GET request paths which start 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}`);
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Route handlers
|
|
|
|
|
|
|
|
You can provide multiple callback functions to handle a request. Invoking ```next()``` function allows to pass the control to subsequent routes.
|
|
|
|
Whether ```next()``` method is not called the handler chain is stoped
|
|
|
|
|
|
|
|
```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);
|
|
|
|
```
|
|
|
|
|
|
|
|
A response to a GET request on path (/example/a) displays:
|
|
|
|
|
|
|
|
```
|
|
|
|
h1!
|
|
|
|
h2!
|
|
|
|
```
|
|
|
|
|
|
|
|
h3 is ignored because ```next()``` function was not invoked.
|
|
|
|
|
|
|
|
### app.route()
|
|
|
|
|
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
### frontexpress.Router
|
|
|
|
|
|
|
|
Use the ```frontexpress.Router``` class to create modular, mountable route handlers.
|
|
|
|
|
|
|
|
Create a router file named ```birds.js``` in the app directory, with the following content:
|
|
|
|
|
|
|
|
```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-09 22:58:03 +00:00
|
|
|
// middleware that is specific to 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-09 22:58:03 +00:00
|
|
|
// react on home page route
|
|
|
|
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-09 22:58:03 +00:00
|
|
|
// react on about route
|
|
|
|
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);
|
|
|
|
```
|
|
|
|
|
|
|
|
The app will now be able to react on requests (/birds) and (/birds/about)
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
[MIT](LICENSE)
|