HTTP Server

Built-in HTTP server with zero configuration.

1 min readEdit this page

Overview

import { Application } from '@restjs/core'
 
const app = new Application()
 
app.get('/hello', (req, res) => {
  res.json({ message: 'Hello from REstJS' })
})
 
app.listen()

Configuration

const app = new Application({
  server: { port: 3000, host: '0.0.0.0' },
  env: 'development',
  logging: true
})

Request object

app.get('/users', (req, res) => {
  req.method   // 'GET'
  req.path     // '/users'
  req.query    // { page: '1' }
  req.params   // { id: '42' }
  req.headers  // request headers
  req.body     // parsed body
  req.ip       // client IP
  req.locals   // middleware data
})

Response object

res.json({ data: 'value' })
res.status(201).json({ created: true })
res.text('Hello')
res.html('<h1>Hello</h1>')
res.redirect('/other')
res.header('X-Custom', 'value').json({ ok: true })

Graceful shutdown

await app.listen()
await app.close()

Note

The server catches all unhandled errors and returns HTTP 500 without crashing.