Helper utilities for Luvit.io servers.
You can install all libraries as one dependency:
lit install voronianski/http-utils
Or install every library separately:
HTTP verbs that Luvit.io aim to support.
lit install voronianski/http-methods
Mime type getter from string.
lit install voronianski/mimes
local mimes = require('mimes')
p(mimes.getType('foo.jpg'))
-- 'image/jpeg'
Match an Express.js-style path string such as /user/:name
and get params.
lit install voronianski/match-path
local matchPath = require('match-path')
local parse = matchPath.compile('/foo/:bar')
-- parsing function
local params = parse('/foo/1')
-- {foo = '1'}
Patches HTTP ServerResponse with 'headers'
event which executes a listener when a response is about to write headers.
lit install voronianski/on-headers-event
-- patches http.ServerResponse:flushHeaders method
require('on-headers-event')
require('http').createServer(function (req, res)
res:on('headers', function
-- do something with headers before they are sent
end)
res:finish()
end):listen(3000)
Patches HTTP ServerResponse with useful response shortcut methods.
lit install voronianski/response-methods
-- adds helpful http.ServerResponse methods
require('response-methods')
local Utopia = require('utopia')
local app = Utopia:new()
app:listen(3000)
res:status(403):finish()
res:status(400):send('Bad Request')
res:status(404):sendFile('/absolute/path/to/404.png')
res:json({user = 'tobi'})
res:status(500):json({error = 'message'})
res:redirect('/foo/bar')
res:redirect('http://example.com')
res:redirect(301, 'http://example.com')
res:redirect('../login')
res:send(Buffer:new('whoop'))
res:send({some = 'json' })
res:send('<p>some html</p>')
res:status(404).send('Sorry, we cannot find that!')
res:status(500).send({error = 'something blew up'})
res:sendStatus(200) -- equivalent to res:status(200):send('OK')
res:sendStatus(403) -- equivalent to res:status(403):send('Forbidden')
res:sendStatus(404) -- equivalent to res:status(404):send('Not Found')
res:sendStatus(500) -- equivalent to res:status(500):send('Internal Server Error')
MIT Licensed
Copyright (c) 2016 Dmitri Voronianski [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.