Welcome to rjweb-server š
Easy and Lightweight Way to create a Web Server in Node.js
Install
npm i rjweb-server
or
yarn add rjweb-server
Typescript
Importing
import * as webserver from "rjweb-server"
Interface for ctr Object
import ctrInterface from "rjweb-server/interfaces/ctr"
routes.set(webserver.types.get, '/hello', async(ctr: ctrInterface) => {
if (!ctr.query.has("name")) return ctr.print('please supply the name query!!')
return ctr.print(`Hello, ${ctr.query.get("name")}! How are you doing?`)
})
Usage
Initialize Server
const webserver = require('rjweb-server')
const routes = new webserver.routeList()
routes.set(webserver.types.get, '/hello', async(ctr) => {
if (!ctr.query.has("name")) return ctr.print('please supply the name query!!')
return ctr.print(`Hello, ${ctr.query.get("name")}! How are you doing?`)
})
routes.set(webserver.types.get, '/hello/:name', async(ctr) => {
return ctr.print(`Hello, ${ctr.param.get("name")}! How are you doing?`)
})
routes.set(webserver.types.post, '/post', async(ctr) => {
return ctr.print(`Hello, ${ctr.reqBody}! How are you doing?`)
})
routes.set(webserver.types.get, '/profile/:user', async(ctr) => {
ctr.setHeader('Content-Type', 'image/png')
return ctr.printFile(`../images/profile/${ctr.param.get('user')}.png`)
})
webserver.start({
bind: '0.0.0.0',
body: 20,
cors: false,
port: 5000,
urls: routes,
proxy: true
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Serve Static Files
const webserver = require('rjweb-server')
const routes = new webserver.routeList()
routes.static('/', './html', {
preload: false,
remHTML: true
})
webserver.start({
bind: '0.0.0.0',
cors: false,
port: 5000,
urls: routes,
proxy: true
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Custom 404 / 500 Page
webserver.start({
bind: '0.0.0.0',
cors: false,
port: port,
urls: routes,
proxy: true,
pages: {
notFound: async(ctr) {
ctr.status(404)
return ctr.print(`page "${ctr.reqUrl.pathname}" not found`)
}, reqError: async(ctr) => {
ctr.status(500)
ctr.print(`ERROR!!! ${ctr.error.message}`)
return console.log(ctr.error)
}
}
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Custom Function on every request
webserver.start({
bind: '0.0.0.0',
cors: false,
port: port,
urls: routes,
proxy: true,
events: {
request: async(ctr) => {
return console.log(`request made to ${decodeURI(ctr.reqUrl.pathname)} by ${ctr.hostIp}`)
}
}
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Cleaning Up Functions
Load Functions from Directory
const webserver = require('rjweb-server')
const routes = new webserver.routeList()
routes.load('./functions')
webserver.start({
bind: '0.0.0.0',
cors: false,
port: 5000,
urls: routes,
proxy: true,
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Making a function File
const webserver = require('rjweb-server')
module.exports = {
type: webserver.types.get,
path: '/say/:word',
async code(ctr) {
const word = ctr.param.get('word')
return ctr.print(`I will say it!!!\n${word}`)
}
}
Full Example
https://replit.com/@RobertJansen/aous
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 Ā© 2022 0x4096.
This project is ISC licensed.