rjweb-server
Advanced tools
Comparing version 0.0.4 to 0.1.0
71
index.js
@@ -13,4 +13,5 @@ const sleep = (milliseconds) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds) | ||
async start(options) { | ||
const urls = options.urls.list() || {} | ||
const urls = options.urls.list() || [] | ||
const bind = options.bind || '0.0.0.0' | ||
const cors = options.cors || false | ||
const port = options.port || 5002 | ||
@@ -20,3 +21,51 @@ | ||
const reqUrl = url.parse(req.url) | ||
let executeUrl = '' | ||
// Cors Headers | ||
let corsHeaders = {} | ||
if (cors) { | ||
corsHeaders = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET', | ||
'Access-Control-Max-Age': 2592000 | ||
} | ||
} | ||
// Check if URL exists | ||
let params = new Map() | ||
let exists = false | ||
const actualUrl = reqUrl.pathname.split('/') | ||
for (let element in urls) { | ||
if (element in urls && element === reqUrl.pathname) { | ||
executeUrl = reqUrl.pathname | ||
exists = true | ||
break | ||
} | ||
element = urls[element] | ||
if (element.array.length !== actualUrl.length) continue | ||
let urlCount = 0 | ||
for (const subUrl of element.array) { | ||
const urlParam = element.array[urlCount] | ||
const reqParam = actualUrl[urlCount] | ||
urlCount++ | ||
if (urlParam === reqParam) { | ||
continue | ||
} else if (urlParam.startsWith(':')) { | ||
params.set(urlParam.replace(':', ''), reqParam) | ||
executeUrl = element.array.join('/') | ||
exists = true | ||
continue | ||
} | ||
continue | ||
} | ||
continue | ||
} | ||
// Create Answer Object | ||
@@ -31,7 +80,7 @@ const headers = new Map() | ||
}; const cookies = new Map() | ||
if (!!req.headers.cookie) { req.headers.cookie.split(`;`).forEach(function(cookie) { | ||
let [ name, ...rest] = cookie.split(`=`) | ||
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() | ||
const value = rest.join('=').trim() | ||
if (!value) return | ||
@@ -45,2 +94,3 @@ cookies.set(name, decodeURIComponent(value)) | ||
cookie: cookies, | ||
param: params, | ||
query: queries, | ||
@@ -58,4 +108,6 @@ | ||
if (urls.hasOwnProperty(reqUrl.pathname) && urls[reqUrl.pathname].type === req.method) { | ||
await urls[reqUrl.pathname].code(ctr).catch((e) => { | ||
if (exists) { | ||
res.writeHead(200, corsHeaders) | ||
await urls[executeUrl].code(ctr).catch((e) => { | ||
res.write(e.message) | ||
@@ -65,8 +117,2 @@ res.end() | ||
} else { | ||
if (urls.hasOwnProperty('*')) { | ||
await urls['*'].code(ctr).catch((e) => { | ||
res.write(e.message) | ||
res.end() | ||
}); return res.end() | ||
} | ||
@@ -80,2 +126,3 @@ if (!options.hasOwnProperty('notfound')) { | ||
res.statusCode = 404 | ||
res.writeHead(404, corsHeaders) | ||
res.write(`[!] COULDNT FIND ${reqUrl.pathname.toUpperCase()}\n[i] AVAILABLE PAGES:\n\n${pageDisplay}`) | ||
@@ -82,0 +129,0 @@ res.end() |
{ | ||
"name": "rjweb-server", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Easy Way to create a Web Server in Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -42,6 +42,11 @@ <h1 align="center">Welcome to rjweb-server 👋</h1> | ||
const port = 5000 | ||
// ctr.param.get... is :name, example /hello/0x4096 | ||
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', | ||
port: port, | ||
cors: false, | ||
port: 5000, | ||
urls: routes | ||
@@ -53,3 +58,3 @@ }).then((res) => { | ||
Custom Not found page | ||
Custom 404 Page | ||
```js | ||
@@ -64,2 +69,3 @@ /* ************ * | ||
bind: '0.0.0.0', | ||
cors: false, | ||
port: port, | ||
@@ -66,0 +72,0 @@ urls: routes, |
@@ -7,3 +7,3 @@ const types = [ | ||
constructor() { | ||
this.urls = {} | ||
this.urls = [] | ||
} | ||
@@ -13,3 +13,8 @@ | ||
if (!types.includes(type)) throw TypeError('No Valid Request Type: ' + type) | ||
this.urls[url] = { type, code } | ||
this.urls[url] = { | ||
hasParams: (url.match(/[\:]([A-Za-z0-9]+)/g) === null ? false : true), | ||
array: url.split('/'), | ||
type, | ||
code | ||
} | ||
}; list() { | ||
@@ -16,0 +21,0 @@ return this.urls |
7935
132
95