The powerful, lightweight HTTP server library for Node
npm i retra
And then use it!
const Retra = require('retra')
const app = new Retra()
app.add('GET', '/test', (req, res) => {
res.status(200).body('Hey there!').end()
})
app.add(/* ? ... ? */(req, res) => {
console.log('name=' + res.query('name'))
// This logs the query string property 'name' from the request.
})
app.add(/* ? ... ? */ async (req, res) => {
const parsed = await req.json()
res.body({
'yourName': parsed.name
}).end()
})
Setting one header:
app.add(/* ? ... ? */ (req, res) => {
res.header('content-type', 'squid').end()
})
Setting many at once:
app.add(/* ? ... ? */ (req, res) => {
res.header({
'content-type': 'squid',
'x-server': 'retra'
}).end()
})
app.add('POST', (req, res) => {
// ...
})
app.add('/squid', (req, res) => {
// ...
})
// ... require fs, path
app.add('/stream', (req, res) => {
res.coreRes.pipe(fs.createReadStream(path.join(__dirname, 'test.txt')))
})
app.listen(8080, () => {
console.log('Listening!')
})
// ... require https
https.createServer(app.externalHandler).listen(443)
app.on('routeError', (err, req, res) => {
if (res.coreRes.finished === false) {
res.writeHead(500)
res.end({
'error': err.message
})
}
// This will respond with the error when one occurs!
})
Host static files in your retra server, easily and efficiently.
Install:
npm i retra-static
Use:
// ... require path module
const static = require('retra-static')
app.use(static(path.join(__dirname, 'static')))
// This will host from the /static directory!