Install
npm i rjweb-server
yarn add rjweb-server
pnpm add rjweb-server
Update Infos
- 0.X | Deprecated
- 1.X | Deprecated
- 2.X | Deprecated
- 3.X | Deprecated
- 4.X | Deprecated
- 5.X | Deprecated
- 6.X | Basic Patches
- 7.X | Patches and Features
Typescript
Custom Properties in HTTP Object
(This also works in JavaScript, just remove the interface logic)
import { Server, HTTPRequestContext, Status } from "rjweb-server"
interface Custom {
count: number
}
const server = new Server({
bind: '0.0.0.0',
port: 5000,
})
server.path('/', (path) => path
.http<Custom>('GET', '/hello', (http) => http
.onRequest(async(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}`)
})
)
.http<Custom, { name: string }>('POST', '/hello', (http) => http
.onRequest(async(ctr) => {
if (!('name' in ctr.body)) return ctr.print('please supply the name property!!')
return ctr.print(`Hello, ${ctr.body.name}! You are Visit nr.${ctr['@'].count}`)
})
)
)
let count = 0
server.on('httpRequest', async(ctr: Ctr) => {
ctr.setCustom('count', ++count)
console.log(`request made to ${decodeURI(ctr.url.pathname)} by ${ctr.client.ip}`)
}).on('httpError', async(ctr: Ctr, error) => {
console.log(`error on path ${decodeURI(ctr.url.pathname)}!!!`)
console.error(error)
ctr.status(Status.INTERNAL_SERVER_ERROR)
ctr.print('server error')
})
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Usage
Initialize Server
const { Server, Status } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true,
body: {
enabled: true,
maxSize: 20,
message: 'Payload too large'
}
})
server.path('/', (path) => path
.http('POST', '/post', (http) => http
.onRequest(async(ctr) => {
return ctr.print(`Hello, ${ctr.body}! How are you doing?`)
})
)
.http('GET', '/profile/<user>', (http) => http
.onRequest(async(ctr) => {
return ctr.printFile(`../images/profile/${ctr.params.get('user')}.png`, { addTypes: true })
})
)
.http('GET', '/reloadserver', (http) => http
.onRequest(async(ctr) => {
if (!ctr.queries.has('password')) return ctr.print('provide the password to do this!!')
if (ctr.queries.get('password') !== 'imApassword123!') return ctr.print('the password is incorrect!!')
setTimeout(() => ctr.controller.reload(), 1000)
return ctr.print('server reloaded')
})
)
.http('GET', '/redirect/<website>', (http) => http
.onRequest(async(ctr) => {
switch (ctr.params.get('website')) {
case "google":
return ctr.redirect('https://www.google.com')
case "youtube":
return ctr.redirect('https://www.youtube.com')
default:
return ctr.print('Im only smart enough to redirect to google and youtube :P')
}
})
)
.ws('/echo', (ws) => ws
.onUpgrade((ctr, end) => {
if (!ctr.queries.has('confirm')) return end(ctr.status(Status.BAD_REQUEST).print('Please dont forget the confirm query!'))
})
.onMessage((ctr) => {
ctr.print(ctr.message)
})
)
.redirect('/googleplz', 'https://www.google.com')
.path('/api', (path) => path
.http('GET', '/', (http) => http
.onRequest((ctr) => ctr.print('welcome to api!'))
)
.path('/v1', (path) => path
.http('GET', '/', (http) => http
.onRequest((ctr) => ctr.print('welcome to v1 api!'))
)
)
.path('/v2', (path) => path
.http('GET', '/', (http) => http
.onRequest((ctr) => ctr.print('welcome to v2 api!'))
)
)
)
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Print to multiple Websockets Periodically
const { Server } = require('rjweb-server')
const { Readable } = require('stream')
const dataStream = (() => {
let i = 0
return new Readable({
objectMode: true,
construct() {
setInterval(() => {
this.push(++i)
}, 1000)
},
})
}) ()
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.path('/', (path) => path
.ws('/infos', (ws) => ws
.onConnect((ctr) => {
ctr.printStream(dataStream, {
destroyAbort: false
})
})
)
)
Print Promises
const { Server } = require('rjweb-server')
const wait = require('timers/promises').setTimeout
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
const handleHello = async() => {
await wait(5000)
return 'You just waited 5 seconds !!'
}
server.path('/wait5sec', (path) => path
.http('GET', '/func', (http) => http
.onRequest((ctr) => {
return ctr.print(handleHello)
})
)
.http('GET', '/promise', (http) => http
.onRequest(async(ctr) => {
await wait(5000)
return ctr.print('You just waited 5 seconds !!')
})
)
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Authenticate Requests
const { Server, Status } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.path('/account', (path) => path
.validate(async(ctr, end) => {
if (!ctr.queries.has('password')) return end(ctr.status(Status.UNPROCESSABLE_ENTITY).print('Passwort Query Missing'))
if (ctr.queries.get('password') !== '123456 or database request or sum') return end(ctr.status(Status.UNAUTHORIZED).print('Unauthorized'))
})
.http('GET', '/', (http) => http
.onRequest(async(ctr) => {
return ctr.print('Account Infos: idk')
})
)
.http('POST', '/edit', (http) => http
.onRequest(async(ctr) => {
return ctr.print(`Edited Account Infos to ${ctr.rawBody}!`)
})
)
)
Use Middleware
const { Server } = require('rjweb-server')
const someMiddleware = require('some-middleware')
const someOtherMiddleware = require('some-other-middleware')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
}, [
someMiddleware.config({}),
someOtherMiddleware.config({})
])
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Serve Static Files
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.path('/', (path) => path
.static('./html', {
hideHTML: true
})
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Add Headers on EVERY Request
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.defaultHeaders((dH) => dH
.add('im-a-header', 'im the value')
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Override Content-Types
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.contentTypes((ct) => cT
.add('.jsn', 'application/json')
.add('.jn', 'application/json')
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Enable Dashboard
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
dashboard: {
enabled: true,
path: '/dashboard',
password: '124'
}
})
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Custom Not Found / Server Error Page
const { Server, Status } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.on('route404', async(ctr) => {
ctr.status(Status.NOT_FOUND)
return ctr.print(`page "${ctr.url.pathname}" not found`)
})
server.on('httpError', async(ctr, error) => {
ctr.status(Status.INTERNAL_SERVER_ERROR)
ctr.print(`ERROR!!! ${error.stack}`)
return console.log(ctr.error)
})
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Custom Function on every request
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
server.on('httpRequest', async(ctr, end) => {
return console.log(`request made to ${decodeURI(ctr.url.href)} by ${ctr.client.ip}`)
})
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Cleaning Up Functions
Load routes from Directory
const { Server } = require('rjweb-server')
const server = new Server({
bind: '0.0.0.0',
cors: false,
port: 5000,
proxy: true
})
module.exports.server = server
server.path('/', (path) => path
.loadCJS('./functions')
.loadESM('./functions')
)
server.start()
.then((s) => {
console.log(`server started on port ${s.port}`)
})
.catch((e) => {
console.error('An Error occured while starting the Server!\n', e)
})
Making a route File
const { server } = require('../index.js')
module.exports = new server.routeFile((file) => file
.http('GET', '/say/<word>', (http) => http
.onRequest((ctr) => {
const word = ctr.params.get('word')
ctr.print(`I will say it!!!!!!!!!\n${word}`)
})
)
)
CLI
Serve a Static Folder using CLI
rjweb serve [path to folder] [arguments]
rjweb serve ./static --port 4444 --hideHTML
Generate a Template using CLI
rjweb generate [path to destination folder]
rjweb generate ./chat-app
View Help
rjweb --help
Official Middlewares
- rjweb-server-ejs (To Render EJS templates easily)
- rjweb-server-ratelimit (To add rate limiting easily)
Full Example
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 Ā© 0x4096.
This project is MIT licensed.