https://github.com/heapwolf/paramify
Parameterized routes without a big bloated router, e.g. "showtimes/:start/:end" and "files/*"
https://github.com/heapwolf/paramify
Last synced: 6 months ago
JSON representation
Parameterized routes without a big bloated router, e.g. "showtimes/:start/:end" and "files/*"
- Host: GitHub
- URL: https://github.com/heapwolf/paramify
- Owner: heapwolf
- Created: 2013-06-03T10:52:11.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-07-12T13:12:29.000Z (over 8 years ago)
- Last Synced: 2025-04-19T07:56:21.690Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 84
- Watchers: 5
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://voltra.co/)
# SYNOPSIS
Parses urls so you can "route"
# MOTIVATION
For when you want url parameters but not a big bloated router
# USAGE
```js
var fs = require('fs')
var http = require('http')
var paramify = require('paramify')
http.createServer(function (req, res) {
var match = paramify(req.url)
res.writeHead(200, {'Content-Type': 'text/plain'})
if (match('intro/:greeting')) {
intro(match.params, res)
}
else if (match('showtimes/:start/:end')) {
showtimes(match.params, res)
}
else if (match('files/*')) {
serveFile(match.params, res)
}
}).listen(1337, '127.0.0.1')
function intro(params, res) {
res.end('Greeting was "' + params.greeting + '"\n')
}
function showtimes(params, res) {
var message = [
'Show starts at', params.start,
'and ends at', params.end
].join(' ') + '\n'
res.end(message)
}
function serveFile(params, res) {
// match.params contains numeric keys for any
// path components matched with *
fs.createReadStream(__dirname + '/static/' + params[0]).pipe(res)
}
console.log('Server running at http://127.0.0.1:1337/')
```
Given the following url
```
http://localhost:1337/showtimes/10:00AM/8:30PM
```
The server would respond with
```
Show starts at 10:00AM and ends at 8:30PM
```
Given the following url
```
http://localhost:1337/intro/Hello%20world!
```
The server would respond with
```
Greeting was "Hello world!"
```
Given the following url
```
http://localhost:1337/files/users/1/description.txt
```
The server would respond with the contents of `static/users/1/description.txt`.