Node.js - http()

Creates a new HTTP proxy, which can be used for wrapping other web frameworks. The example below shows wrapping an express application.

import { http } from '@nitric/sdk'
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
http(app, () => {
console.log(`Application started`)
})

Parameters

  • Name
    app
    Required
    Required
    Type
    NodeApplication | ((port: number, callback?: () => void) => ServerType | Promise<ServerType>)
    Description

    Accepts an object that has a listen function on it, or pass the listener function itself. A listener function is a function that accepts a port and an optional callback, and is used to start the application.

  • Name
    callback
    Optional
    Optional
    Type
    () => void
    Description

    A callback that is passed into the listen function when the application is started.

Examples

Before testing or deploying these services you'll need to make sure you have a nitric.yaml file in the base of the project.

name: project-name
services:
- match: services/*.ts
start: npm run dev:services $SERVICE_PATH

Wrap a Nest.js application

For a Nest application, point your handler at the main.ts file.

name: project-name
services:
- match: src/main.ts
start: nest start
import { http } from '@nitric/sdk'
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
async function bootstrap(port: number) {
const app = await NestFactory.create(AppModule)
return await app.listen(port)
}
http(bootstrap)

Wrap an Express application

import { http } from '@nitric/sdk'
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
http(app)

Wrap a Koa application

import { http } from '@nitric/sdk'
import Koa from 'koa'
const app = new Koa()
app.use(async (ctx) => {
ctx.body = 'Hello World'
})
http(app)
Last updated on Oct 14, 2024