Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

trouter

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trouter

WIP

  • 0.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
449K
increased by8.01%
Maintainers
1
Weekly downloads
 
Created

What is trouter?

Trouter is a lightweight and fast router for Node.js that allows you to define routes and handle HTTP requests. It is designed to be minimalistic and efficient, making it suitable for building small to medium-sized web applications and APIs.

What are trouter's main functionalities?

Basic Routing

This feature allows you to define basic routes for handling HTTP GET requests. The code sample demonstrates how to create a simple route that responds with 'Hello, world!' when accessed.

const Trouter = require('trouter');
const router = new Trouter();

router.get('/hello', (req, res) => {
  res.end('Hello, world!');
});

// Simulate a request
const req = { method: 'GET', url: '/hello' };
const res = { end: console.log };
router.find(req.method, req.url).handlers[0](req, res);

Route Parameters

This feature allows you to define routes with parameters. The code sample demonstrates how to create a route that captures a user ID from the URL and responds with the user ID.

const Trouter = require('trouter');
const router = new Trouter();

router.get('/user/:id', (req, res) => {
  const { id } = req.params;
  res.end(`User ID: ${id}`);
});

// Simulate a request
const req = { method: 'GET', url: '/user/123', params: { id: '123' } };
const res = { end: console.log };
router.find(req.method, req.url).handlers[0](req, res);

Middleware Support

This feature allows you to use middleware functions in your routes. The code sample demonstrates how to create a logger middleware that logs the request method and URL before handling the request.

const Trouter = require('trouter');
const router = new Trouter();

const logger = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};

router.use(logger);
router.get('/hello', (req, res) => {
  res.end('Hello, world!');
});

// Simulate a request
const req = { method: 'GET', url: '/hello' };
const res = { end: console.log };
const { handlers } = router.find(req.method, req.url);
handlers[0](req, res, () => handlers[1](req, res));

Other packages similar to trouter

FAQs

Package last updated on 05 Nov 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc