🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

larvitrouter

Package Overview
Dependencies
Maintainers
6
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitrouter

URL router

latest
Source
npmnpm
Version
5.1.132
Version published
Maintainers
6
Created
Source

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

Keywords

router

FAQs

Package last updated on 17 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts