https://github.com/herber/pico
A simple & fast http abstraction
https://github.com/herber/pico
fast http server
Last synced: 7 months ago
JSON representation
A simple & fast http abstraction
- Host: GitHub
- URL: https://github.com/herber/pico
- Owner: herber
- License: mit
- Created: 2017-06-22T12:47:38.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-05T06:04:13.000Z (over 8 years ago)
- Last Synced: 2025-03-12T01:24:43.116Z (about 1 year ago)
- Topics: fast, http, server
- Language: JavaScript
- Size: 44.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# pico
[](https://bytes.gq) [](https://coveralls.io/github/tobihrbr/pico?branch=master) [](https://travis-ci.org/herber/pico) [](https://ci.appveyor.com/project/tobihrbr/pico)
> A simple & fast http abstraction
### [Examples](https://github.com/tobihrbr/pico/tree/master/examples)
## Install
```
$ npm install --save pico-http
```
## Usage
### Requests
```js
const pico = require('pico-http');
// Middleware
pico.use((req, res, next) => {
console.log(req.method + ' ' + req.url + ' on ' + new Date());
next();
});
// Serve get requests to `/`
pico.get('/', (req, res) => {
// Set contenttype
res.contentType('text/html');
// Send response
res.send('Hello World');
});
// Serve post requests to `/api`
pico.post('/api', (req, res) => {
const o = {
user: 'test'
};
res.contentType('application/json');
res.send(JSON.stringify(o));
});
// Catch every unhandled request
pico.serve('/*', '_', (req, res) => {
res.status(404);
res.send('Custom 404');
});
// Listen to port
pico.listen(3000);
```
## API
### listen(port)
Listen to http requests on port
#### port
Type: `number`
The port that should be used.
### use(cb)
Add middleware. Middleware will be executed on every request before specific route handling
#### cb
Type: `function`
Will be executed on request
##### Parameters:
- req
Type: `object`
Contains request information
- res
Type: `object`
Use `res.send()` to send something back to the client
- next
Type: `function`
Execute next middleware / request handler
### serve(route, method, cb)
#### route
Type: `string` or `regexp`
Allowed route
#### method
Type: `string`
Http method(eg. `GET`, `POST`)
#### cb
Type: `function`
Will be executed if `route` and `method` match
##### Parameters:
- req
Type: `object`
Contains request information
- res
Type: `object`
Use `res.send()` to send something back to the client
## License
MIT © [Tobias Herber](https://tobihrbr.com)