Ey is an ergonomic and fast server+router, built on uWebSockets (for now). It has a simple interface embracing todays JS features.
import ey from 'ey'
const app = ey()
app.get('/', r => r.end('Hello World'))
const { port } = await app.listen(process.env.PORT)
The request object contains all relavant data and methods to handle incoming requests and methods to respond as desired.
app.get(r => {
r // is the request object
})
The HTTP verb of the request.
Contains the actual url sent by the client
Contains the relative url for the specific handler
An object containing headers. If multiple headers are found the value will be a comma separated list of values.
A URLSearchParams object for the query string. This is a getter so the URLSearchParams object will be created lazily.
An object of the matched routing params like.
/authors/:author/books/:book
= { author: 'Murray', book: 'Ethics of Liberty' }
A function which reads the incoming body and transforms it to an optional type text
or json
. If no type is specificed a Buffer
will be returned.
Returns an object representing the cookie
Implement middleware with all
. Ey forwards the request to the next handler, unless a response has begun.
app.all((r, next) => {
r.headers.authorization
? r.token = r.headers.authorization.split(' ')[1]
: r.statusEnd(401) // request ends here
})
app.all(r => {
r.token
})
There are various ways to optimize your routes by being more specific in their definition.
You can specify which headers a route will use, up front, to prevent reading and loading unnecessary headers. Be aware that this is not possible for route handlers that come after other async handlers.
const app = ey({ headers: ['content-type'] })
app.get('/login', { headers: ['authorization'] }, r => {
r.headers['content-type'] // string
r.headers['authorization'] // string
r.headers['accept'] // undefined
})
Any middleware that accepts 2 arguments will be registrered as an error handler and receive the error as the first argument, and the request object as the second. This will allow you to log errors and reply accordingly. If no error handler makes a response the default error handler will reply with 500 Internal Server Error
and call console.error
.
app.all((error, r) => {
connected = true
res.end(await ...)
})
Routes are matched according to the order in which they are defined. This is important to support middleware with less specific route matching.
hej | med | yo |
---|---|---|
/user |
will only match requests to the url /user |
/user*
All requests that begins with /user
/user/*
All requests that begins with /user/
*/user
All requests that ends with /user
All requests with one path segment, setting
r.params = { user }
All requests with 3 segments that has
posts
in the middle, settingr.params = { user, post }
// GET /u25
app.get('/:id', r =>
r.params.id // The actual id value
)
// GET /?sort=-price
app.get(r => {
req.query.sort // -price
})
// POST /user { name: 'Murray' }
app.post('/user', async r => {
const body = await r.body('json')
, user = await sql`insert into users ${ sql(body) }`
r.json(user, 201) // r.json sets Content-Type: application/json header
})
app.get('/favicon.ico', r => r.file('/public/favicon.ico'))
app.all('/node_modules', r.files('/node_modules'))
// POST /file data
app.post('/file', async r => {
await new Promise((resolve, reject) =>
r.readable.pipe(fs.createWriteStream('...'))
.on('finish', resolve)
.on('error', reject)
)
r.end('Done')
})
// GET /old-link
app.get('/old-link', async r => {
r.statusEnd(302, { location: '/new-link' })
})
app.all(r => {
const session = await sql`select * from sessions where key = ${
r.headers.authorization.split(' ')[0]
}`
})
Ey simplifies the express routing model, by removing request
and next
.