zuper
v0.2.0
Published
Zuper HTTP Server
Downloads
1
Readme
zuper
Usage
import { Server } from 'http'
import { Application, Router } from 'zuper'
const server = new Server()
const app = new Application()
const router = new Router()
const log = (req, next) => {
console.log(req.method, req.url)
return next(req)
}
const getName = (req, next) => next(req.params.name)
const greet = (name = 'world') => `Hello, ${name}!`
const notFound = () => ({
statusCode: 404,
headers: {},
body: '404 Not Found'
})
router.get('/hello/:name?', getName, greet)
app.use(log)
app.use(router.middleware)
app.use(notFound)
server.on('request', app.handleRequest)
server.listen(80, '127.0.0.1', () => {
console.log('Server running at http://localhost/')
})
$ curl -i http://localhost/hello
HTTP/1.1 200 OK
Content-Length: 13
Content-Type: text/plain
Hello, world!
$ curl -i http://localhost/hello/John
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: text/plain
Hello, John!