Go to file
2016-07-23 00:52:13 +02:00
docs added docs 2016-07-23 00:02:34 +02:00
lib Added browser history management issue #1 2016-07-17 13:37:35 +02:00
test Added browser history management issue #1 2016-07-17 13:37:35 +02:00
.babelrc initial commit 2016-06-26 12:10:37 +02:00
.eslintignore made eslint stricter - added unit tests to cover sample in README 2016-07-10 00:58:03 +02:00
.eslintrc made eslint stricter - added unit tests to cover sample in README 2016-07-10 00:58:03 +02:00
.gitignore added support of request with querystring and or anchor 2016-07-02 15:45:54 +02:00
.npmignore exposed project as an NPM package 2016-06-28 13:36:39 +02:00
.travis.yml initial commit 2016-06-26 12:10:37 +02:00
LICENSE Initial commit 2016-06-26 11:34:45 +02:00
package.json update version 2016-07-17 14:01:16 +02:00
README.md Update README.md 2016-07-23 00:52:13 +02:00

frontexpress

Minimalist front end router framework à la express

Build Status Code Climate Coverage Status Coverage Status

import frontexpress from 'frontexpress';
const app = frontexpress();

// listen HTTP GET request on path (/hello)
app.get('/hello', (req, res) => {
  document.querySelector('.content').innerHTML = '<p>Hello World</p>';
});

// 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();
});

// start listening frontend application requests
app.listen();

Installation

$ npm install frontexpress

Quick Start

The quickest way to get started with frontexpress is to clone the frontexpress-demo repository.

Tests

Clone the git repository:

$ git clone git@github.com:camelaissani/frontexpress.git
$ cd frontexpress

Install the dependencies and run the test suite:

$ npm install
$ npm test

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.

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).

The following examples illustrate how to define simple routes.

Listen an HTTP GET request on URI (/):

app.get('/', (req, res) => {
  window.alert('Hello World');
});

Listen an HTTP POST request on URI (/):

app.post('/', (req, res) => {
  window.alert('Got a POST request at /');
});

Route paths

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.

This route path matches all GET request paths which start with (/api/):

app.get(/^api\//, (req, res) => {
  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

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().

app.route('/book')
 .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') });

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:

import frontexpress from 'frontexpress';

const router = frontexpress.Router();

// middleware that is specific to this router
router.use((req, res, next) => {
  console.log(`Time: ${Date.now()}`);
  next();
});

// react on home page route
router.get('/', (req, res) => {
  document.querySelector('.content').innerHTML = '<p>Birds home page</p>';
});

// react on about route
router.get('/about', (req, res) => {
  document.querySelector('.content').innerHTML = '<p>About birds</p>';
});

export default router;

Then, load the router module in the app:

import birds  from './birds';
...
app.use('/birds', birds);

The app will now be able to react on requests (/birds) and (/birds/about)

API

Class Method
Frontexpress
frontexpress()
frontexpress.Router
frontexpress.Middleware
Application
set(setting, value)
listen(callback)
route(uri)
use(uri, middleware)
get(uri, middleware)
post(uri, middleware)
put(uri, middleware)
delete(uri, middleware)
httpGet(request, success, failure)
httpPost(request, success, failure)
httpPut(request, success, failure)
httpDelete(request, success, failure)
Router
use(middleware)
all(middleware)
get(uri, middleware)
post(uri, middleware)
put(uri, middleware)
delete(uri, middleware)
Middleware
entered(request)
exited(request)
updated(request, response)
failed(request, response)
next()

License

MIT