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

itty-router

Package Overview
Dependencies
Maintainers
2
Versions
265
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

itty-router - npm Package Compare versions

Comparing version 2.1.3 to 2.1.4

2

package.json
{
"name": "itty-router",
"version": "2.1.3",
"version": "2.1.4",
"description": "Tiny, zero-dependency router with route param and query parsing.",

@@ -5,0 +5,0 @@ "main": "./dist/itty-router.min.js",

@@ -24,13 +24,18 @@ ![Logo][logo-image]

// register some routes
router
.get('/todos', () => new Response('Todos Index!')) // GET index
.get('/todos/:id', request => new Response(`Todo #${request.params.id}`)) // GET item
.post('/todos', async request => { // POST new item
const content = await request.json()
// GET collection index
router.get('/todos', () => new Response('Todos Index!'))
return new Response('Creating a new Todo with following payload: ' + JSON.stringify(content))
})
.all('*', () => new Response('Not Found.', { status: 404 })) // 404 otherwise
// GET item
router.get('/todos/:id', ({ params }) => new Response(`Todo #${params.id}`))
// POST to the collection (we'll use async here)
router.post('/todos', async request => {
const content = await request.json()
return new Response('Creating Todo: ' + JSON.stringify(content))
})
// 404 for everything else
router.all('*', () => new Response('Not Found.', { status: 404 }))
// attach the router "handle" to the event handler

@@ -70,3 +75,3 @@ addEventListener('fetch', event =>

const router = Router() // no "new", as this is not a real ES6 class/constructor!
const router = Router() // no "new", as this is not a real class/constructor!
```

@@ -97,5 +102,5 @@

// Example outputs (using route handler from step #2 above):
// GET /todos/jane/13 --> { user: 'jane', item: '13', query: {} }
// GET /todos/jane --> { user: 'jane', query: {} }
// GET /todos/jane?limit=2&page=1 --> { user: 'jane', query: { limit: '2', page: '2' } }
// GET /todos/jane/13 --> { user: 'jane', item: '13', query: {} }
// GET /todos/jane --> { user: 'jane', query: {} }
// GET /todos/i?limit=2&page=1 --> { user: 'i', query: { limit: '2', page: '2' } }
```

@@ -108,3 +113,3 @@

// lets save a missing handler
const missingHandler = new Response('That resource was not found.', { status: 404 })
const missingHandler = new Response('Not found.', { status: 404 })

@@ -111,0 +116,0 @@ // create a parent router

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