trasherdk / hyper-express

High performance Node.js webserver with a simple-to-use API powered by uWebsockets.js under the hood.
MIT License
0 stars 0 forks source link

Snippet: Proxy / Redirect API endpoint #25

Open trasherdk opened 1 month ago

trasherdk commented 1 month ago
import HyperExpress from 'hyper-express'
import { request } from 'node:http'

const server = new HyperExpress.Server()

server.any('/api/v1', (request_, response) => {
  request_.pipe(
    request(
      'http://localhost:8081/api/v1',
      {
        method: request_.method,
        headers: request_.headers,
      },

      (readable) => {
        readable.pipe(response)
      },
    ),
  )
})

const PORT = process.env['PORT'] ?? '3000'

await server.listen(PORT, () => {
  console.log(`Service endpoint = http://localhost:${PORT}`)
})