Welcome to rjweb-server š
Easy Way to create a web Server in node.js
Install
npm i rjweb-server
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!!')
ctr.print(`Hello, ${ctr.query.get("name")}! How are you doing?`)
})
routes.set(webserver.types.get, '/hello/:name', async(ctr) => {
ctr.print(`Hello, ${ctr.param.get("name")}! How are you doing?`)
})
webserver.start({
bind: '0.0.0.0',
cors: false,
port: 5000,
urls: routes
}).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')
webserver.start({
bind: '0.0.0.0',
cors: false,
port: 5000,
urls: routes
}).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,
pages: {
notFound: async(ctr) {
ctr.status(404)
ctr.print(`page "${ctr.requestPath.pathname}" not found`)
}, reqError: async(ctr) => {
ctr.status(500)
ctr.print(`ERROR!!! ${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,
events: {
request: async(ctr) => {
console.log(`request made to ${ctr.requestPath.pathname}`)
}
}
}).then((res) => {
console.log(`webserver started on port ${res.port}`)
})
Author
š¤ 0x4096
š¤ Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a āļø if this project helped you!
š License
Copyright Ā© 2022 0x4096.
This project is ISC licensed.