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

microrouter

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microrouter - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

10

lib/index.js

@@ -5,10 +5,12 @@ const { getParamsAndQuery } = require('../utils')

const methodFn = method => (path, handler) => (req, res) => {
const methodFn = method => (path, handler) => {
if (!path) throw new Error('You need to set a valid path')
if (!handler) throw new Error('You need to set a valid handler')
const { params, query } = getParamsAndQuery(path, req.url)
return (req, res) => {
const { params, query } = getParamsAndQuery(path, req.url)
if (params && req.method === method) {
return handler(Object.assign({}, req, { params, query }), res)
if (params && req.method === method) {
return handler(Object.assign({}, req, { params, query }), res)
}
}

@@ -15,0 +17,0 @@ }

@@ -50,1 +50,18 @@ const listen = require('test-listen')

})
test('not found route', async t => {
const hello = () => 'Hello world'
const notfound = () => 'Not found'
const routes = router(
get('/hello', hello),
get('/*', notfound)
)
const url = await server(routes)
const { body: helloBody } = await got(`${url}/hello`)
const { body: notfoundBody } = await got(`${url}/api`)
t.is(helloBody, 'Hello world')
t.is(notfoundBody, 'Not found')
})

6

package.json
{
"name": "microrouter",
"version": "1.0.1",
"version": "1.1.0",
"main": "lib/index.js",

@@ -12,8 +12,8 @@ "scripts": {

"dependencies": {
"route-parser": "^0.0.5"
"url-pattern": "^1.0.3"
},
"devDependencies": {
"micro": "^7.0.6",
"ava": "^0.18.2",
"got": "^6.7.1",
"micro": "^7.0.6",
"test-listen": "^1.0.1",

@@ -20,0 +20,0 @@ "xo": "^0.17.1"

@@ -10,3 +10,3 @@ :station: _**Micro Router -**_ A tiny and functional router for Zeit's [micro](https://github.com/zeit/micro)

- **Tiny**. Just 23 lines of code.
- **Functional**. Write your method using functions.
- **Functional**. Write your http methods using functions.
- **Async**. Design to use with `async/await`

@@ -31,4 +31,8 @@

const notfound = () =>
send(res, 404, 'Not found route')
module.exports = router(
get('/hello/:who', hello)
get('/hello/:who', hello),
get('/*', notfound)
)

@@ -67,3 +71,3 @@ ```

A simple route path like that you can set any parameters using a `:` notation.
A simple route path that you can set any parameters using a `:` notation.
The `req` parameter from `handler` will return this parameters as a object.

@@ -70,0 +74,0 @@

const { parse } = require('url')
const Route = require('route-parser')
const UrlPattern = require('url-pattern')
const getParamsAndQuery = (path, url) => {
const { query } = parse(url, true)
const route = new Route(path)
const params = route.match(url)
const getParamsAndQuery = (pattern, url) => {
const { query, pathname } = parse(url, true)
const route = new UrlPattern(pattern)
const params = route.match(pathname)

@@ -9,0 +9,0 @@ return { query, params }

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