Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mike1pol/node-router
Small router for node.js http/https
https://github.com/mike1pol/node-router
node node-module node-router nodejs nodejs-server router
Last synced: 15 days ago
JSON representation
Small router for node.js http/https
- Host: GitHub
- URL: https://github.com/mike1pol/node-router
- Owner: mike1pol
- License: mit
- Created: 2019-06-18T07:57:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:52:00.000Z (about 2 years ago)
- Last Synced: 2024-12-08T15:04:17.725Z (28 days ago)
- Topics: node, node-module, node-router, nodejs, nodejs-server, router
- Language: JavaScript
- Size: 352 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Node-Router
Small router for node.js http/https[![Build Status](https://travis-ci.org/mike1pol/node-router.svg?branch=master)](https://travis-ci.org/mike1pol/node-router)
[![npm version](https://badge.fury.io/js/%40mikepol%2Fnode-router.svg)](https://badge.fury.io/js/%40mikepol%2Fnode-router)
[![codecov](https://codecov.io/gh/mike1pol/node-router/branch/master/graph/badge.svg)](https://codecov.io/gh/mike1pol/node-router)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)## Install
`npm install @mikepol/node-router`## Example
```js
const http = require('http')
const router = require('@mikepol/node-router')
const routes = [
{
method: 'get',
url: '/',
handler: (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('get /')
}
},
{
method: 'post',
url: '/',
handler: (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('post /')
}
},
{
method: 'get',
url: /^\/page\/(\d)/,
handler: (req, res) => {
console.log(req.route.match);
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('post /')
}
},
{
url: '/all',
handler: (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('/all')
}
},
{
notFound: true,
handler: (req, res) => {
res.statusCode = 404
res.setHeader('Content-Type', 'text/plain')
res.end('404 - Not found')
}
}
]const srv = http.createServer(router(routes))
srv.listen(3000, (err) => {
if (err) {
console.error('server listen error:', err)
}
console.log(`server started on http://localhost:3000`)
})
```