Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
http-routify
Advanced tools
Routify is a minimal http routing module for node.
It mixes the simplicity of tiny-route with the parameter support of paramify.
npm install http-routify
var http = require('http')
var Stack = require('stack')
var router = require('http-routify')
http
.createServer(
Stack(
router.get('/', (req, res) => res.end('Root')),
router.post('/blog/posts/', (req, res) => {
res.statusCode = 201
res.end('Created blog post')
}),
router.get('/blog/posts/:slug', (req, res) => {
res.end(`We support parameters as well: ${req.params.slug}`)
})
)
)
.listen(1337)
The example uses Stack and requires node 8 or above.
npm start
Open up a browser or command line and test the routes on http://localhost:1337
Running the tests requires node 8 or above.
npm test
npm run test:watch
router.verb(path,handler[,middleware])
path: <string>
A path pattern relative to the root. Example: /api/users/:id
handler: <function>
The handler to execute when the request matches. See Handlers.
middleware: <array>
[Optional] Array of middleware to execute before executing the handler. Overrides all middleware globally applied to the router. See Middleware.
The router currently supports the following HTTP verbs
Matches HTTP GET requests
Matches HTTP POST requests
Matches HTTP PUT requests
Matches HTTP DELETE requests
router.all
is a bit special. It matches any HTTP verb and only matches the url against the path.
Each route handler needs to be of the form:
function handler (req, res, next) {
// Either handle the request here using `req` and `res`
// or call `next()` to pass control to next route
}
The next
function is only required in order to delegate handling of a request to the next route in the stack and may be omitted.
The router supports defining middleware that wraps around each route handler, creating a chain of behaviors to be executed. Middleware can be defined globally and overridden on each route.
router.use(middleware)
middleware: <function>
A middleware function needs to be in the form of:
function middleware (inner) {
return function (req, res, next) {
inner(req, res, next)
}
}
In general, a middleware should always call any inner functions (middleware or handler) passed to it. However, there are exceptions where you might want to skip calling inner behaviors and end the request in your middleware.
To run code after the inner behavior has executed you may use async/await in handlers and middleware. See example below.
router.use(inner => {
return (req, res, next) => {
console.log('Before handler')
inner(req, res, next)
}
})
router.use(inner => {
return async (req, res, next) => {
console.log('Before handler')
await inner(req, res, next)
console.log('After handler')
}
})
Please report any bugs by opening an issues on Github.
https://github.com/hij1nx/paramify
FAQs
Routify is a minimal http routing module for node.js.
The npm package http-routify receives a total of 8 weekly downloads. As such, http-routify popularity was classified as not popular.
We found that http-routify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.