5.1.81 • Published 9 days ago

larvitrouter v5.1.81

Weekly downloads
147
License
ISC
Repository
github
Last release
9 days ago

Build Status

URL router

Route an URL to a controller, template and/or a static file.

Auto resolves files like so:

  • /foo might resolve to controllerPath: foo.js and templatePath: foo.ejs
  • /css/style.css might resolve to staticPath: css/style.css
  • / resolves to controllerPath: default.js and templatePath: default.tmpl

This behaviour can be changed with customized options.

Installation

npm i larvitrouter

Usage

All options passed here are optional and the given ones are the default that will be used if they are omitted.

Paths are relative to application root as first priority. If nothing is found there, all modules will be tested as relative to this path to try to find a matching file. The modules are searched in the order given in package.json dependencies.

Simple, use default options:

const Router = require('larvitrouter'),
      router = new Router();

Use custom options (the defaults are used in this example):

const LUtils = require('larvitutils');
const lUtils = new lUtils();
const Router = require('larvitrouter');
const router = new Router({
	'basePath':        process.cwd(),
	'cacheMax':        1000
	'paths':           {
		'controller': {
			'path': 'controllers',
			'exts': 'js'
		},
		'static': {
			'path': 'public',
			'exts': false // Match all
		},
		'template': {
			'path': 'public/templates',
			'exts': ['tmpl', 'tmp', 'ejs', 'pug']
		}
	},
	'log':    new lUtils.Log(),
	'routes': [{
		'regex':          '^/$',
		'controllerPath': 'default.js',
		'templatePath':   'default.tmpl'
	}]
});

Resolve a path

const Router = require('larvitrouter');
const router = new Router();
const http   = require('http');

http.createServer(function(req, res) {
	router.resolve(req.url, function(err, result) {
		if (err) throw err;

		// A static file was found
		if (result.staticFilename !== undefined) {
			console.log('static path: ' + result.staticPath);
			console.log('static full path: ' + result.staticFullPath);
		}

		// A controller was found
		if (result.controllerPath) {
			console.log('controller path: ' + result.controllerPath);
			console.log('controller full path: ' + result.controllerFullPath);
		}

		// A template was found
		if (result.templatePath) {
			console.log('template path: ' + result.templatePath);
			console.log('template full path: ' + result.templateFullPath);
		}

		res.end('Resolved stuff, see console output for details');
	})
}).listen(8001);

Changelog

5.1.0

  • Added cache layer

5.0.0

  • Removed larvitfs support
5.1.81

9 days ago

5.1.80

16 days ago

5.1.79

23 days ago

5.1.78

1 month ago

5.1.77

1 month ago

5.1.76

2 months ago

5.1.75

2 months ago

5.1.74

3 months ago

5.1.73

3 months ago

5.1.72

4 months ago

5.1.71

4 months ago

5.1.70

4 months ago

5.1.69

4 months ago

5.1.68

4 months ago

5.1.67

4 months ago

5.1.66

5 months ago

5.1.65

5 months ago

5.1.64

5 months ago

5.1.63

6 months ago

5.1.62

6 months ago

5.1.61

6 months ago

5.1.60

6 months ago

5.1.59

7 months ago

5.1.58

7 months ago

5.1.57

7 months ago

5.1.56

8 months ago

5.1.55

8 months ago

5.1.54

8 months ago

5.1.53

8 months ago

5.1.52

9 months ago

5.1.51

9 months ago

5.1.50

10 months ago

5.1.49

10 months ago

5.1.48

10 months ago

5.1.47

11 months ago

5.1.46

11 months ago

5.1.45

11 months ago

5.1.44

11 months ago

5.1.43

12 months ago

5.1.42

12 months ago

5.1.41

12 months ago

5.1.40

1 year ago

5.1.39

1 year ago

5.1.38

1 year ago

5.1.37

1 year ago

5.1.36

1 year ago

5.1.35

1 year ago

5.1.34

1 year ago

5.1.33

1 year ago

5.1.32

1 year ago

5.1.31

1 year ago

5.1.30

1 year ago

5.1.29

1 year ago

5.1.28

1 year ago

5.1.27

1 year ago

5.1.26

1 year ago

5.1.25

1 year ago

5.1.24

1 year ago

5.1.23

2 years ago

5.1.22

2 years ago

5.1.21

2 years ago

5.1.20

2 years ago

5.1.19

2 years ago

5.1.18

2 years ago

5.1.17

2 years ago

5.1.16

2 years ago

5.1.15

2 years ago

5.1.14

2 years ago

5.1.5

2 years ago

5.1.13

2 years ago

5.1.12

2 years ago

5.1.11

2 years ago

5.1.10

2 years ago

5.1.9

2 years ago

5.1.8

2 years ago

5.1.7

2 years ago

5.1.6

2 years ago

5.1.4

2 years ago

5.1.3

2 years ago

5.1.2

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.4

7 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.0.1-beta

8 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.30

9 years ago

0.0.29

9 years ago

0.0.28

9 years ago

0.0.27

9 years ago

0.0.26

9 years ago

0.0.25

9 years ago

0.0.24

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13-beta

9 years ago

0.0.12-beta

9 years ago

0.0.11-beta

9 years ago

0.0.10-beta

9 years ago

0.0.9-beta

9 years ago

0.0.8-beta

9 years ago

0.0.7-beta

9 years ago

0.0.6-beta

9 years ago

0.0.5-beta

9 years ago

0.0.4-beta

9 years ago

0.0.3-beta

9 years ago

0.0.2-beta

9 years ago