rjweb-server
Advanced tools
Comparing version 0.0.1 to 0.0.2
51
index.js
@@ -1,2 +0,3 @@ | ||
const { URLgen } = require('./utils/URLgen') | ||
const sleep = (milliseconds) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds) | ||
const { RouteList } = require('./utils/RouteList') | ||
const http = require('node:http') | ||
@@ -6,3 +7,6 @@ const url = require('node:url') | ||
module.exports = { | ||
URLgen, | ||
RouteList, | ||
types: { | ||
get: 'GET' | ||
}, | ||
@@ -15,4 +19,38 @@ async start(port, urls) { | ||
if (urls.hasOwnProperty(reqUrl.pathname)) { | ||
await urls[reqUrl.pathname](req, res).catch((e) => { | ||
// Create Answer Object | ||
const headers = new Map() | ||
Object.keys(req.headers).forEach(function(header) { | ||
headers.set(header, req.headers[header]) | ||
}); headers.delete('cookie') | ||
const queries = new Map() | ||
for (const [query, value] of new URLSearchParams(reqUrl.search)) { | ||
queries.set(query, value) | ||
}; const cookies = new Map() | ||
if (!!req.headers.cookie) { req.headers.cookie.split(`;`).forEach(function(cookie) { | ||
let [ name, ...rest] = cookie.split(`=`) | ||
name = name?.trim() | ||
if (!name) return | ||
const value = rest.join(`=`).trim() | ||
if (!value) return | ||
cookies.set(name, decodeURIComponent(value)) | ||
})} | ||
const ctr = { | ||
// Properties | ||
header: headers, | ||
cookie: cookies, | ||
query: queries, | ||
// Variables | ||
hostIp: req.socket.remoteAddress, | ||
hostPort: req.socket.remotePort, | ||
requestPath: reqUrl, | ||
// Functions | ||
print(msg) { res.write(msg) }, | ||
status(code) { res.statusCode = code } | ||
} | ||
if (urls.hasOwnProperty(reqUrl.pathname) && urls[reqUrl.pathname].type === req.method) { | ||
await urls[reqUrl.pathname].code(ctr).catch((e) => { | ||
res.write(e.message) | ||
@@ -23,3 +61,3 @@ res.end() | ||
if (urls.hasOwnProperty('*')) { | ||
await urls['*'](req, res).catch((e) => { | ||
await urls['*'].code(ctr).catch((e) => { | ||
res.write(e.message) | ||
@@ -32,5 +70,6 @@ res.end() | ||
Object.keys(urls).forEach(function(url) { | ||
pageDisplay = pageDisplay + `[-] ${url}` | ||
pageDisplay = pageDisplay + `[-] [${urls[url].type}] ${url}` | ||
}) | ||
res.statusCode = 404 | ||
res.write(`[!] COULDNT FIND ${reqUrl.pathname.toUpperCase()}\n[i] AVAILABLE PAGES:\n\n${pageDisplay}`) | ||
@@ -37,0 +76,0 @@ res.end() |
{ | ||
"name": "rjweb-server", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Easy Way to create a Web Server in Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,6 +35,7 @@ <h1 align="center">Welcome to rjweb-server 👋</h1> | ||
const webserver = require('rjweb-server') | ||
const urls = new webserver.URLgen() | ||
const urls = new webserver.RouteList() | ||
urls.set('/hello', async(req, res) => { | ||
res.write('hello world!') | ||
// ctr.query.get... is ?name= | ||
urls.set('/hello', async(ctr) => { | ||
ctr.print(`hello, ${ctr.query.get("name")}!`) | ||
}) | ||
@@ -41,0 +42,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5146
77
64