A simple & fast http abstraction
$ npm install --save pico-http
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);
Listen to http requests on port
Type: number
The port that should be used.
Add middleware. Middleware will be executed on every request before specific route handling
Type: function
Will be executed on request
-
req
Type:object
Contains request information -
res
Type:object
Useres.send()
to send something back to the client -
next
Type:function
Execute next middleware / request handler
Type: string
or regexp
Allowed route
Type: string
Http method(eg. GET
, POST
)
Type: function
Will be executed if route
and method
match
-
req
Type:object
Contains request information -
res
Type:object
Useres.send()
to send something back to the client
MIT © Tobias Herber