npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dir-routes

v1.0.8

Published

Use your directory structure as routes

Downloads

14

Readme

dir-routes

Build Status

Build your routes like in old fashioned way to deliver resources through HTTP. It's pretty much a concept than a package, think on the file path as a route and the file name as a HTTP method. Take a look here to see why use it.

What it does?

This package creates a folder named "routes" in the base directory of your application, then puts a 'Hello World!' example and an index.js file inside it, if there was already a folder named "routes" it just creates the index.js file. The real magic happens inside the routes folder, the index file iterates through all directories and subdirectories mapping all paths and files to an express router.

If you don't want to use this package you could simply use the index.js to var router = require(./your-routes-folder).

It supports route parameters but Windows doesn't allow : in folder's name, use @ instead

RegExp aren't supported

How files should be

get.js

// your import
var mongoose = require('mongoose');

module.exports = function(req, res){
	// your stuff
};

If you want a post just change the file name to post.js or any method that you need, eg delete.js.

Usage:

var express = require('express'),
	app = express(),
	router = require('dir-routes');

app.use(router);

Example App:

Simple login application describing how it works, just download the zip and:

> unzip example.zip
> node example
< Listening on port 8080

Access via: http://127.0.0.1:8080

Example:

App File

var express = require('express'),
http = require('http'),
app = express(),
router = require('dir-routes');

app.param('param', function(req, res, next){
	next();
});

app.use(router);

app.use(function(req, res) {
	res.status(404).end('404: Page not Found');
});

var httpServer = http.createServer(app);

httpServer.listen(8080, function() {
	console.log('Listening on port %d', httpServer.address().port);
});

Folder Structure

-| node_modules
-| routes
 | -| @param
 |  | -| something
 |  |  | -| get.js
 |  |  | -| post.js
 |  | -| other
 |  |  | -| post.js
 | -| new
 |  | -| :param2
 |  |  | -| get.js
 |  | -| get.js
 | -| get.js
-| app.js

Routes generated

GET  /
GET  /:param/something/
POST /:param/something/
POST /:param/other/
GET  /new/:param2/
GET  /new/

Same as

var express = require('express'),
http = require('http'),
app = express(),
router = require('dir-routes');

app.param('param', function(req, res, next){
	next();
});

app.get('/', function(){
	// TO-DO
});

app.get('/:param/something/', function(){
	// TO-DO
});

app.post('/:param/something/', function(){
	// TO-DO
});

app.post('/:param/other/', function(){
	// TO-DO
});

app.get('/new/:param2/', function(){
	// TO-DO
});

app.get('/new/', function(){
	// TO-DO
});

app.use(function(req, res) {
	res.status(404).end('404: Page not Found');
});

var httpServer = http.createServer(app);

httpServer.listen(8080, function() {
	console.log('Listening on port %d', httpServer.address().port);
});

Log

While mapping it appends messages to a string that is acessible via router.log. Example:

var router = require('dir-routes');
console.log(router.log);

Inspired by @olado's doT.js express example.