- Lightweight and fast
- Streamlined feature set, sufficient for most applications
- Supports
express
(andconnect
) compatible middleware - Both CommonJS and ES module support via tsukuru
import { Server } from 'httpanda';
import cors from 'cors';
const server = new Server();
server.use(cors())
.get('/hello', (req, res) => res.end('Hello World!'))
.get('/users/:id', (req, res) => res.end(`Welcome, user ${req.params.id}!`))
.listen(3000).then(() => console.log('Listening on port 3000')).catch(e => console.error(e));
Creates a new instance of HTTPanda.
All options are optional, you can just use new Server()
to use the default options.
Available options:
Your own HTTP server instance. Can also take a https.Server
to support SSL.
An error handler that takes the following arguments:
e
- the error objectreq
- the request object (an instance of the internalRequest
type)res
- the response object (an instance of the internalResponse
type, currently equivalent tohttp.ServerResponse
)next
- a function you can call to ignore the error and keep walking the middleware chain
const server = new Server({
server: https.createServer({ /* ... certificate stuff ... */ }),
onError: (e, res) => {
console.log(e);
res.statusCode = e.code || 500;
res.end(JSON.stringify(e));
}
})
Listens to the given port. The returned Promise
resolves with the Server
instance on success, or rejects in case the port is in use or could otherwise not be bound.
Other forms of this as shown in the node net
documentation are also supported, except the callback is always replaced by the returned Promise
.
Chains a set of middlewares on the given path, or on any path if none is given. Express middlewares are supported!
Adds a callback to a specific path. Parameters can be specified on the path with a leading colon and are available on req.params
:
server.get('/users/:id', (req, res) => res.end(`Welcome, user ${req.params.id}!`))
If you pass undefined
as method, the callback will execute on any method.
A shortcut to server.add(undefined, path, ...callbacks)
.
A shortcut to server.add('GET', path, ...callbacks)
.
Other shortcuts that work the same way:
server.head
server.options
server.post
server.put
server.patch
server.delete
server.connect
server.trace