rjweb-server
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -175,2 +175,4 @@ "use strict"; | ||
rawRes: res, | ||
// Custom Variables | ||
'@': {}, | ||
// Functions | ||
@@ -180,2 +182,5 @@ setHeader(name, value) { | ||
return ctr; | ||
}, setCustom(name, value) { | ||
ctr['@'][name] = value; | ||
return ctr; | ||
}, print(msg) { | ||
@@ -182,0 +187,0 @@ switch (typeof msg) { |
@@ -6,3 +6,3 @@ /// <reference types="node" /> | ||
import { types } from "./types"; | ||
export default interface ctr { | ||
export default interface ctr<Custom = any> { | ||
/** A Map of all Headers */ readonly header: Map<any, any>; | ||
@@ -22,5 +22,7 @@ /** A Map of all Cookies */ readonly cookie: Map<any, any>; | ||
/** Set an HTTP Header to add */ setHeader: (name: string, value: string) => ctr; | ||
/** Set a Custom Variable */ setCustom: (name: string, value: any) => ctr; | ||
/** Print a Message to the Client */ print: (msg: any) => ctr; | ||
/** The Request Status to Send */ status: (code: number) => ctr; | ||
/** Print the Content of a File to the Client */ printFile: (path: string) => ctr; | ||
/** Custom Variables */ '@': Custom; | ||
} | ||
@@ -27,0 +29,0 @@ export interface ctrError extends ctr { |
{ | ||
"name": "rjweb-server", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Easy and Lightweight Way to create a Web Server in Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -52,2 +52,38 @@ <h1 align="center">Welcome to rjweb-server 👋</h1> | ||
Custom Properties in Ctr Object | ||
(This also works in JavaScript, just remove the interface logic) | ||
```ts | ||
import * as webserver from "rjweb-server" | ||
import { ctr as ctrPreset } from "rjweb-server/interfaces" | ||
interface Custom { | ||
count: number | ||
} | ||
interface ctr extends ctrPreset<Custom> {} | ||
const routes = new webserver.routeList() | ||
routes.set(webserver.types.get, '/hello', async(ctr: ctr) => { | ||
if (!ctr.query.has("name")) return ctr.print('please supply the name query!!') | ||
return ctr.print(`Hello, ${ctr.query.get("name")}! You are Visit nr.${ctr['@'].count}`) | ||
}) | ||
let count = 0 | ||
webserver.start({ | ||
bind: '0.0.0.0', // The IP thats bound to | ||
port: 5000, // The Port which the Server runs on | ||
urls: routes, // The Routes Object | ||
events: { | ||
request: { | ||
ctr.setCustom('count', ++count) | ||
console.log(`request made to ${decodeURI(ctr.reqUrl.pathname)} by ${ctr.hostIp}`) | ||
} | ||
} | ||
}).then((res) => { | ||
console.log(`webserver started on port ${res.port}`) | ||
}) | ||
``` | ||
## Usage | ||
@@ -54,0 +90,0 @@ |
@@ -207,2 +207,5 @@ import ctr, { ctrError } from "./interfaces/ctr" | ||
// Custom Variables | ||
'@': {}, | ||
// Functions | ||
@@ -212,2 +215,5 @@ setHeader(name: string, value: string) { | ||
return ctr | ||
}, setCustom(name: string, value: any) { | ||
ctr['@'][name] = value | ||
return ctr | ||
}, print(msg: any) { | ||
@@ -214,0 +220,0 @@ switch (typeof msg) { |
@@ -5,3 +5,3 @@ import { Server, IncomingMessage, ServerResponse } from "http" | ||
export default interface ctr { | ||
export default interface ctr<Custom = any> { | ||
/** A Map of all Headers */ readonly header: Map<any, any> | ||
@@ -22,5 +22,8 @@ /** A Map of all Cookies */ readonly cookie: Map<any, any> | ||
/** Set an HTTP Header to add */ setHeader: (name: string, value: string) => ctr | ||
/** Set a Custom Variable */ setCustom: (name: string, value: any) => ctr | ||
/** Print a Message to the Client */ print: (msg: any) => ctr | ||
/** The Request Status to Send */ status: (code: number) => ctr | ||
/** Print the Content of a File to the Client */ printFile: (path: string) => ctr | ||
/** Custom Variables */ '@': Custom | ||
} | ||
@@ -27,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
85095
1337
312