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
375
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rjweb-server

Easy and Lightweight Way to create a Web Server with Many Features

  • 2.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
increased by27.3%
Maintainers
1
Weekly downloads
 
Created
Source

Welcome to rjweb-server v2 👋

Version Documentation Maintenance Socket Badge

Easy and Lightweight Way to create a Web Server in Node.js

🏠 Homepage

Install

npm i rjweb-server

or

yarn add rjweb-server

or

pnpm add rjweb-server

Typescript

Importing

import * as webserver from "rjweb-server"

Interface for ctr Object

import { ctr } from "rjweb-server/interfaces"

const routes = new webserver.routeList()

routes.set(webserver.types.get, '/hello', async(ctr: ctr) => {
  if (!ctr.queries.has("name")) return ctr.print('please supply the name queries!!')

  return ctr.print(`Hello, ${ctr.queries.get("name")}! How are you doing?`)
})

// ...

Custom Properties in Ctr Object (This also works in JavaScript, just remove the interface logic)

import * as webserver from "rjweb-server"
import { ctr as ctrPreset, ctrFile as ctrFilePreset } from "rjweb-server/interfaces"
interface Custom {
  count: number
}

interface ctr<HasError = false, Body = any> extends ctrPreset<Custom, HasError, Body> {}
interface ctrFile<Body = any> extends ctrFilePreset<Custom, Body> {}

const routes = new webserver.routeList()

routes.set(webserver.types.get, '/hello', async(ctr: ctr) => {
  if (!ctr.queries.has("name")) return ctr.print('please supply the name queries!!')

  return ctr.print(`Hello, ${ctr.queries.get("name")}! You are Visit nr.${ctr['@'].count}`)
})

let count = 0
routes.event('request', async(ctr: ctr) => {
  ctr.setCustom('count', ++count) 

  console.log(`request made to ${decodeURI(ctr.url.pathname)} by ${ctr.client.ip}`)
})

routes.event('error', async(ctr: ctr<true>) => {
  console.log(`error on path ${decodeURI(ctr.url.pathname)}!!!`)
  console.error(ctr.error.stack)

  ctr.status(500)
  ctr.print('server error')
})

webserver.start({
  bind: '0.0.0.0', // The IP thats bound to
  port: 5000, // The Port which the Server runs on
  routes: routes // The Routes Object
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
})

Function File

import * as webserver from "rjweb-server"
import { ctr as ctrPreset, ctrFile as ctrFilePreset } from "rjweb-server/interfaces"
interface Custom {
  count: number
} // You should import / export this Interface Stuff (look examples section)

interface Body {
  username?: string
}

interface ctr<HasError = false, Body = any> extends ctrPreset<Custom, HasError, Body> {}
interface ctrFile<Body = any> extends ctrFilePreset<Custom, Body> {}

export = {
  method: webserver.types.post,
  path: '/v2/account',

  async code(ctr) {
    if (!('username' in ctr.body)) return ctr.print('no username in body!!')

    ctr.status(204).print(ctr.body.username)
  }
} as ctrFile<Body>

Usage

Initialize Server

const webserver = require('rjweb-server')
const routes = new webserver.routeList()

// ctr.queries.get... is ?name=
routes.set(webserver.types.get, '/hello', async(ctr) => {
  if (!ctr.queries.has("name")) return ctr.print('please supply the name Query!!')

  return ctr.print(`Hello, ${ctr.queries.get("name")}! How are you doing?`)
})

// ctr.params.get... is :name, example /hello/0x4096
routes.set(webserver.types.get, '/hello/:name', async(ctr) => {
  return ctr.print(`Hello, ${ctr.params.get("name")}! How are you doing?`)
})

// ctr.params.get... is :name, example /hello/0x4096
routes.set(webserver.types.post, '/post', async(ctr) => {
  return ctr.print(`Hello, ${ctr.body}! How are you doing?`)
})

routes.set(webserver.types.get, '/profile/:user', async(ctr) => {
  return ctr.printFile(`../images/profile/${ctr.params.get('user')}.png`, { addTypes: true })
})

webserver.start({
  bind: '0.0.0.0', // The IP thats bound to
  body: 20, // The Max POST Body in MB
  cors: false, // If Cors Headers will be added
  port: 5000, // The Port which the Server runs on
  routes: routes, // The Routes Object
  proxy: true // If enabled, alternate IPs will be shown
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Print Promises

const webserver = require('rjweb-server')
const routes = new webserver.routeList()
const wait = require('timers/promises').setTimeout

// this request will load for 5 seconds and send the returned value
const handleHello = async() => {
  await wait(5000)
  return 'You just waited 5 seconds !!'
}
routes.set(webserver.types.get, '/hello', async(ctr) => {
  return ctr.print(handleHello)
})

webserver.start({
  bind: '0.0.0.0', // The IP thats bound to
  body: 20, // The Max POST Body in MB
  cors: false, // If Cors Headers will be added
  port: 5000, // The Port which the Server runs on
  routes: routes, // The Routes Object
  proxy: true // If enabled, alternate IPs will be shown
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Serve Static Files

const webserver = require('rjweb-server')
const routes = new webserver.routeList()

routes.static('/', './html', {
  preload: false, // If enabled will load every static files content into Memory
  remHTML: true // If enabled will remove the html ending from files when serving
}) // The html folder is in the root directory

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: 5000,
  routes: routes,
  proxy: true
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Enable Dashboard

const routes = new webserver.routeList()

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: 5000,
  routes: routes,
  proxy: true,
  dashboard: {
    enabled: true,
    path: '/dashboard' // The Dashboad is now accessible at /dashboard
  }
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Custom Not Found / Server Error Page

const routes = new webserver.routeList()

routes.event('notfound', async(ctr) => {
  ctr.status(404)
  return ctr.print(`page "${ctr.url.pathname}" not found`)
})

routes.event('error', async(ctr) => {
  ctr.status(500)
  ctr.print(`ERROR!!! ${ctr.error.message}`)
  return console.log(ctr.error)
})

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: port,
  routes: routes,
  proxy: true
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Custom Function on every request

const routes = new webserver.routeList()

routes.event('request', async(ctr) => {
  return console.log(`request made to ${decodeURI(ctr.url.href)} by ${ctr.client.ip}`)
  // DO NOT write any data or end the request
})

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: port,
  routes: routes,
  proxy: true
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Rate Limits

With Database (EXAMPLE, PLEASE EDIT)

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: port,
  routes: routes,
  proxy: true,
  rateLimits: {
    enabled: true,
    message: 'You are being rate limited',
    list: [
      {
        path: '/auth',
        times: 5,
        timeout: 10000
      },
      {
        path: '/fetch',
        times: 3,
        timeout: 5000
      }
    ], functions: {
      set: async(key, value) => (await db.queries('update ratelimits set value = ? where key = ?;', [value, key])),
      get: async(key) => ((await db.queries('select value from ratelimits where key = ?;', [key])).data.rows[0].value)
    }
  }
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

With Map

const rateLimits = new Map()

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: port,
  routes: routes,
  proxy: true,
  rateLimits: {
    enabled: true,
    message: 'You are being rate limited',
    list: [
      {
        path: '/auth',
        times: 5,
        timeout: 10000
      },
      {
        path: '/fetch',
        times: 3,
        timeout: 5000
      }
    ], functions: rateLimits
  }
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Cleaning Up Functions

Load Functions from Directory

const webserver = require('rjweb-server')
const routes = new webserver.routeList()

routes.load('./functions') // The functions folder is in the root directory

webserver.start({
  bind: '0.0.0.0',
  cors: false,
  port: 5000,
  routes: routes,
  proxy: true,
}).then((res) => {
  console.log(`webserver started on port ${res.port}`)
}).catch((err) => {
  console.log(err)
})

Making a function File

const webserver = require('rjweb-server')

/** @type {import('rjweb-server/interfaces').ctrFile} */
module.exports = {
  method: webserver.types.get,
  path: '/say/:word',

  async code(ctr) {
    const word = ctr.params.get('word')

    return ctr.print(`I will say it!!!\n${word}`)
  }
}

Full Example

Javascript

https://replit.com/@RobertJansen/aous

Typescript

https://replit.com/@RobertJansen/aous-ts

Author

👤 0x4096

🤝 Contributing

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

Show your support

Give a Star if this project helped you!

📝 License

Copyright © 2023 0x4096.
This project is MIT licensed.

Keywords

FAQs

Package last updated on 22 Jan 2023

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