Skip to content

Examples

Eric M. Dantas edited this page May 16, 2018 · 10 revisions

A bunch of examples demonstrating how to use aliv, from the CLI to a simple node module.

CLI - with inline arguments

$ aliv --port 9999 --pathIndex src --only src

CLI - with .alivrc file

$ aliv
{
  "port": 9999,
  "pathIndex": "src",
  "only": src 
} 

Node module

"use strict";

// pull the dependency
const Server = require('aliv');

// start server
let _aliv = new Server({quiet: true});

_aliv.start();

_aliv.on('client-connected', () => {
  // client connected
});

_aliv.on('file-changed', (info) => {
  // file changed, info.path
});

_aliv.on('browser-open', (info) => {
  // browser open called
});

_aliv.on('reload', () => {
  // reload called
});

Node module with middlewares

"use strict";

// pull the dependency
const Server = require('aliv');

// start server
let _aliv = new Server({quiet: true});

_aliv.use((done) => {
   // do something
   done()
})

_aliv.use((done) => {
   setTimeout(() => {
     // does something else
     done()
   }, 1000)
})

_aliv.start();

Gulp - using aliv's watcher

"use strict";

const gulp = require('gulp');

// pull the dependency
const Server = require('aliv');

// grab the instance of the server
let _aliv = new Server({quiet: true, port: 1234}); 

gulp.task('default', () => {
    // when the user runs 'gulp', we start the server  
    // and the file changes that happen will trigger the browser
    // reload automatically
	_aliv.start();
});

Gulp - using gulp's watcher

"use strict";

const gulp = require('gulp');

// pull the dependency
const Server = require('aliv');

// grab the instance of the server
let _aliv = new Server({quiet: true, port: 1234, watch: false}); 

gulp.task('reload', () => {
  // reload the browsers
  _aliv.reload();
});

gulp.task('default', () => {
    // when the user runs 'gulp', we start the server  
	_aliv.start();

    // we call the 'reload' task everytime a css, js or html changes
    // based on gulp's implementation
	gulp.watch(['**/*.js', '**/*.css', '**/*.html'], ['reload']);
});

Clone this wiki locally