https://github.com/tdb-alcorn/nice-router
Route requests to handlers nicely.
https://github.com/tdb-alcorn/nice-router
http node router server
Last synced: about 1 month ago
JSON representation
Route requests to handlers nicely.
- Host: GitHub
- URL: https://github.com/tdb-alcorn/nice-router
- Owner: tdb-alcorn
- Created: 2016-12-07T18:41:24.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-20T00:11:35.000Z (about 9 years ago)
- Last Synced: 2025-11-27T09:20:20.435Z (4 months ago)
- Topics: http, node, router, server
- Language: JavaScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nice-router
A nice, simpler router for handling those pesky HTTP(S) requests.
## Example
```javascript
var Router = require("nice-router");
var router = new Router();
router.addRoute("/ping", "GET", function(req, res) {
res.writeHead(200);
res.end("pong");
});
router.addStatic("/smiley.jpg", "image/jpeg");
router.listen(8080);
```
### Basic HTTPS example
To use this example you will need to generate self-signed certificates `server.key` and
`server.crt`. If you are unsure about how to do that, see this [Stack
Overflow](http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl).
```javascript
var fs = require("fs");
var Router = require("nice-router");
var router = new Router();
router.useHTTPS({
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem"),
};
router.addRoute("/amIEncrypted", "GET", function(req, res) {
res.writeHead(200);
res.end("yes!");
});
router.listen(8080);
```
## Methods
- `#addRoute(path, method, handler)`: Adds a route to the router which will respond to `method`
requests to `path` by doing `handler`. The signature of handlers is `handler(request, response,
headers, query, body)`. The `request` and `response` objects are the same as those from the node
`http`/`https` modules. `headers` and `query` are basic key/value objects. `body` is a Buffer, so
call `body.toString("utf-8")` if you just want a string.
- `#listen(port) -> Promise`: Start the server on the specified port, which must be open.
- `#close -> Promise`: Stop the server.
- `#useHTTPS(options[, callback])`: Switches the server to using HTTPS, restarting if the server was already running. `options` is the same as the options parameter in node's `https.createServer`. Accepts an optional `callback` which will be called if the server is restarted.
- `#addStatic(path, contentType)`: Adds a handler which responds to `GET` requests at `path` with
the contents of the file at `path`.