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

folder-routing

v0.2.0

Published

A folder based router.

Downloads

13

Readme

Banner image

folder-routing

Downloads Version

Folder based routing for APIs.

folder-routing is a thin wrapper around ExpressJS which is based on folder routing, similar to app router in NextJS.

Highlights

  • Hassle free start-up
  • Request and Response objects
  • Asynchronous functions
  • Various route-types supported
    • Nested Routes
    • Dynamic Routes
    • Catch-all segment routes
  • Network-wide hosting
  • Beautiful CLI for folder-structure

Install

  1. Initialize the project

    npm init -y

    Or you can run it with your personal flags and fashion. Also, the project comes only in ES Modules flavor. IMPORTANT: Hence, remember to include type: module inside package.json file.

  2. Add the dependency

    npm install folder-routing

    Add folder-routing to the set of main dependency.

  3. Start the server

    npx folder-routing

    Voila! You just ran a folder-routing API. The server will be available across connected networks and the CLI will output the exact address.

Usage

Whole of the folder-routing revolves around the concept of dividing routes among files and intercept requests to produce results. Every endpoint is represented by a single file.

The framework looks for default export of asynchronous functions, from file named index.js, to fill in the response. It uses express.app.send under the hood. Thus, JSON, strings and other data type can be handled readily.

Also, the asynchronous nature allows you to make other fetch requests from databases, APIs or even JSON files. Hence, features like rate limiting, API keys and authorization can be implemented easily.

Steps to create an API:

  1. the app folder,

    The app folder serves as / route. Every other sub-directory is the next endpoint. Create app folder directly under your root folder. Nest folders to create nested routes for the API.

    /
    └── app/
    	├── users/             # Simple Route
    	│   └── index.js
    	├── [userID]/          # Dynamic Route
    	│   └── index.js
    	├── badge/
    	│	└── [...rest]/    # Catch-all Segment Route
    	│		└── index.js
    	└── index.js

    Types of supported routes:

    • Normal routes(You already know them)
    • Dynamic Routes [route_name] Enclose folder name within square brackets to convert it into dynamic route which can be accessed under params in request object.
    • Catch-all Segments [...route_name] It will catch all nested routes after this folder, which can be accessed under request object.

    Fancy routes related data can be accessed via request object for more info consult Express Documentation.

  2. the index.js file,

    Inside of a index.js file should be a default export function, which should return the data to be displayed. The function can be async in nature. The function will get the request object as an arguement to the function.

    export default function Users(request) {
    	return [
    		{
    			id: "abc",
    			name: "Ashish Khare",
    		},
    	];
    }

    If you want to intercept a particular method, you can write specific methods for it. eg: Here is the example for post requests,

    export function POST(request) {
    	// body ...
    }

    Similarly, you can write named methods for GET, PUT and DELETE. The functions should be in upper-case. Also, the default export wires to the GET request, by default.

  3. the data.json file,

    Alternatively, you can use data.json file to serve data. The framework will automatically serve the data from the file. The file should be named data.json.

    [
    	{
    		"id": "abc",
    		"name": "Ashish Khare"
    	}
    ]
  4. the npx command,

    Once you're done arranging the data across folders. Run the following command to start up the server.

    npx folder-routing

    Now, you've a working API.

Tests

I've written a handful of tests to check the functionality of the framework. You can run the tests using the following command: (It uses the app folder described in the root directory.)

npm test

All tests can be found in __test__ folder, and the tests are divided into two categories:

  1. Positive Tests These are listed inside API.test.js file. They mainly test the functionality of the framework and the API for positive results. Like if a route were to return any data, then they check for it.
  2. Negative Tests These are listed inside NEGATIVE.test.js file. Contrary to positive tests, these check for pre-made errors that should be silent and should not disrupt entire flow. Like if a route were to return an error, then they check for it.

TO-DO

  • [ ] add Args support(especially for "host across network" feature)
  • [x] POST requests (along with PUT, DELETE, GET)
  • [x] JSON file support
  • [x] tons of Tests(API testing) (Working on it...)
  • [x] maybe more Documentation
  • [ ] set up the Website
  • [ ] [not advised] write custom HTTP server

Maintainers