Neru

website·Indexed 31 days ago

https://neru.dev

A framework-agnostic file routing system that adds fast file routing to any web framework. It provides official adapters for Express, Fastify, and Hapi JS, allowing developers to define routes as JavaScript or TypeScript files that export handlers for HTTP verbs.

Tokens
2.9K
Snippets
18
Records
29
Agent score
51%

What's inside Neru

  1. Define dynamic route parameters in Neru

    Use the [param] syntax in route filenames or directory names to capture dynamic segments of a URL. For example, a file named [id].js inside a posts/ directory will match URLs like /post/1 or /post/2, capturing the value as the id parameter.
    posts/
      [id].js
  2. Install and set up Neru with Fastify

    To use Neru with Fastify, install the core neru package, the fastify server, and the @nerujs/fastify adapter. Initialize the server by passing the Fastify instance and the adapter to the neru function.
    npm install fastify neru @nerujs/fastify
    import { adapter } from '@nerujs/fastify';
    import fastify from 'fastify';
    import { neru } from 'neru';
    
    const server = fastify({
        logger: true,
    });
    
    await neru({
        adapter,
        server,
    });
    
    server.listen({ port: 4000 }, (error, address) => {
        console.log(error ? `Error: ${error}` : `Online: ${address}`);
    });
  3. Capture multiple URL segments using Spread Parameters

    Spread Parameters (also known as Catch-All Parameters) use the [...param] syntax to capture one or more segments of a URL dynamically. This is useful for deep paths like file systems. Note that support for spread parameters depends on the specific adapter being used.
    // Matches /test/something, /test/something/else, etc.
    test2/
      [...parts].js
    
    // Complex example: GitHub-style file viewer
    [org]/
      [repo]/
        tree/
          [branch]/
            [...file].js
  4. Define routes using the Express adapter

    Routes in Neru using the Express adapter can be defined in three ways: using the provided route helper for built-in type safety, as basic functions, or by manually typing the handler using Express's RequestHandler type.
    // 1. Type Safe (Recommended)
    import { route } from '@nerujs/express';
    export const GET = route((req, res) => {
        res.send('Hello World');
    });
    
    // 2. Basic
    export const GET = (req, res) => {
        res.send('Hello World');
    };
    
    // 3. Manual Typing (TypeScript/JSDoc)
    /** @type {import('express').RequestHandler} */
    export const GET = (req, res) => {
        res.send('Hello World')
    }
  5. Handle all HTTP methods with an ALL handler

    Some framework adapters support an ALL handler that responds to any HTTP request method. Note that some adapters only allow the ALL handler if it is the only handler exported in the route file. Check your specific adapter documentation for support details.
    export const ALL = (req, res) => {
        res.send(`${req.method} Hello World`)
    }
  6. Define application routes using Route Files in Neru

    In Neru, routes are defined by the file system structure. A route is a JavaScript or TypeScript file that exports route handlers. The file and directory names determine the URL path, where an index file corresponds to the root (/) of that directory level.
  7. Implement type safety for route handlers

    Route handlers can be made type-safe using JSDoc types or the route helper function provided by official Neru adapters. The route function is the recommended way to ensure type safety for request and response objects.
    import { route } from '@nerujs/express';
    
    // Using the route helper for automatic type safety
    export const GET = route((req, res) => {
        res.send('Hello World')
    })
    
    // Alternatively, using JSDoc for manual typing
    /** @type {import('express').RequestHandler} */
    export const POST = (req, res) => {
        res.send('Hello World')
    }
  8. Migrate Route Handlers to uppercase in Neru v1.0.0

    In Neru v1.0.0, route handlers have been changed from lowercase (e.g., get, post, del) to uppercase (e.g., GET, POST, DELETE). You must update the exported constant names in your route files to match these uppercase HTTP methods.
    import { route } from '@nerujs/express';
    
    // Change lowercase handlers to uppercase
    export const GET = route((req, res) => {
        res.send('Hello World')
    });
    
    export const DELETE = route((req, res) => {
        res.send('Hello World')
    });
  9. Define HTTP route handlers in Neru

    In Neru, route files export functions named after HTTP verbs (e.g., GET, POST, PUT, DELETE) to handle incoming requests. The specific request and response objects depend on the framework adapter being used.
    // test.js
    
    export const GET = (req, res) => {
        res.send('GET Hello World');
    }
    
    export const POST = (req, res) => {
        res.send('POST Hello World');
    }
    
    export const DELETE = (req, res) => {
        res.send('DELETE Hello World');
    }
  10. Select an official neru adapter

    The neru ecosystem provides official adapters for popular Node.js web frameworks. Depending on your project's requirements, you can use the following official packages:
    # For Express
    npm install @nerujs/express
    
    # For Hapi
    npm install @nerujs/hapi
    
    # For Fastify
    npm install @nerujs/fastify
  11. Add Neru to an existing project using Express

    To integrate Neru into an existing project, install the core library and the corresponding adapter for your framework. For Express, use @nerujs/express. You must initialize Neru by passing the server instance, the adapter, and the path to your routes directory. The routes directory should be located within the src folder.
    npm install neru @nerujs/express
    import { adapter } from '@nerujs/express';
    import { neru } from 'neru';
    
    // For express, server is defined as: const server = express()
    await neru({
        server,
        adapter,
        routes: 'src/routes',
    });
  12. Install and initialize Neru with the Express adapter

    To use Neru with Express, install the required packages and initialize the neru function by passing the Express server instance, the Express adapter, and the path to your routes directory.
    npm install express neru @nerujs/express
    import { adapter } from '@nerujs/express';
    import express from 'express';
    import { neru } from 'neru';
    
    const server = express();
    
    await neru({
        server,
        adapter,
        routes: 'src/routes',
    });
    
    server.listen(4000, () => {
        console.log('Online on http://localhost:4000');
    });