Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More ā†’
Socket
Sign inDemoInstall
Socket

rjweb-server

Package Overview
Dependencies
Maintainers
1
Versions
373
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rjweb-server

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

  • 9.5.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
Ā 
Created
Source

rjweb-server

RJWEB Icon

Easy and Lightweight Web Server Library

šŸ  Homepage šŸ” Documentation

lines of code files

Disclaimer

This Package is intended to be used in the backend.

  • āœ”ļø ESM
  • āœ”ļø CJS
  • āœ”ļø Typescript

Install

npm install rjweb-server
yarn add rjweb-server
pnpm add rjweb-server
bun install rjweb-server

You will need a runtime to actually use the server. (adapt command to your package manager)

npm install @rjweb/runtime-bun # for bun users
npm install @rjweb/runtime-node # for nodejs users (who couldve guessed, may also work with deno though)

Migrating

From 8.X to 9.X

Example Usage

All Utilities and examples for specific classes are available in the Documentation.

This Web Server Library is not for pure beginners. For anything slightly advanced you will need basic knowledge of servers. Same with the documentation, very few is provided directly. You should be able to find everything in TSDoc @example sections of the respective Method.

Basic Server

import { Server } from "rjweb-server"
import { Runtime } from "@rjweb/runtime-node"

const server = new Server(Runtime, {
  port: 8080 // https://server.rjweb.dev/types/ServerOptions
})

server.path('/', (path) => path
  .http('GET', '/', (http) => http
    .onRequest((ctr) => {
      return ctr.print('Hello World!')
    })
  )
)

server.start().then((port) => {
  console.log(`Server started on port ${port}!`)
})

Serve Side Events

import { Server } from "rjweb-server"
import { Runtime } from "@rjweb/runtime-node"
import { time } from "@rjweb/utils"

const server = new Server(Runtime, {
  port: 8080 // https://server.rjweb.dev/types/ServerOptions
})

server.path('/', (path) => path
  .http('GET', '/timer', (http) => http
    .document({
      parameters: [
        {
          in: 'query',
          name: 'seconds',
          required: false,
          schema: {
            type: 'number'
          }
        }
      ]
    })
    .onRequest((ctr) => {
      ctr.headers.set('content-type', 'text/event-stream')
      ctr.headers.set('x-accel-buffering', 'no')
      ctr.headers.set('cache-control', 'no-cache')
      ctr.headers.set('connection', 'keep-alive')

      let seconds: number | null = null
      const secondsInt = parseInt(ctr.queries.get('seconds', ''))

      if (!isNaN(secondsInt) && secondsInt >= 1) seconds = secondsInt

      ctr.printChunked((print) => new Promise<void>(async(end) => {
        print('retry: 10000\n\n')

        let count = 0
        const interval = setInterval(() => {
          count++

          print(`data: ${count}\n\n`)
          if (seconds && seconds <= count) {
            clearInterval(interval)
            end()
          }
        }, time(1).s())

        ctr.$abort(() => {
          clearInterval(interval)
          end()
        })
      }))
    })
  )
  .http('GET', '/openapi', (http) => http
    .onRequest((ctr) => {
      return ctr.print(server.openAPI('Example Server', '1.0.0', { url: 'http://localhost.8080' }))
    })
  )
)

server.start().then((port) => {
  console.log(`Server started on port ${port}!`)
})

Websockets

import { Server } from "rjweb-server"
import { Runtime } from "@rjweb/runtime-node"
import { time } from "@rjweb/utils"

const server = new Server(Runtime, {
  port: 8080 // https://server.rjweb.dev/types/ServerOptions
})

server.path('/', (path) => path
  .ws('/echo', (ws) => ws
    .onMessage(async(ctr) => {
      await ctr.print(ctr.messageType(), ctr.rawMessageBytes())
    })
  )
)

server.start().then((port) => {
  console.log(`Server started on port ${port}!`)
})

Author

šŸ‘¤ 0x7d8 šŸŒ Website ā­ GitHub

šŸ¤ Contributing

Contributions, issues and feature requests are welcome! Feel free to check issues page.

Show your support

Give a ā­ļø if this project helps you!

šŸ“ License

Copyright Ā© 2024 0x7d8. This project is MIT licensed.

Keywords

FAQs

Package last updated on 15 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with āš”ļø by Socket Inc