Simple Deno Web Framework
import {AppServe,Request,Router} from "https://deno.land/x/[email protected]/mod.ts"
AppServe(async()=>{
Router.get('/',()=>{
Request.toResponse({
content:'text/plain',
body:`Hello World`
})
})
},{port:8080})
Router Setup
import {AppServe,Request,Router} from "./mod.ts" AppServe(async()=>{ Router.get('/',()=>{ Request.toResponse({ content:'text/plain', body:`Hello World` }) }) },{port:8080})
Available Router Methods
Router.get('/path',()=>{}); Router.post('/path',()=>{}); Router.put('/path',()=>{}); Router.patch('/path',()=>{}); Router.delete('/path',()=>{}); Router.option('/path',()=>{});
Router parameter
Router.get(`/Article/:slug/Category/:category`,()=>{})Example Url
http://localhost:8080/Article/How-to-make-web/Category/web-tech
Adding middleware to the router
Router.get('/path',()=>{},[func1,func2...]);Example
function func1(next:any){ console.log("middleware 1"); next() } function func2(next:any){ console.log("middleware 2"); next() } Router.get('/',()=>{ Request.toResponse({ content:'text/plain', body:`Hello World` }) },[func1,func2])
Name Property Return path:string
Get path of URL method:string
Get HTTP request method params:object
Get Router parameters body:any
Access Body Request addResponseHeader(init?:HeadersInit)Add response headers
Example :
Request.addResponseHeader({ "Access-Control-Allow-Headers" : "*", "Access-Control-Allow-Origin" : "*" })getHeader(name:string)Get header value
Example :
Request.getHeader("User-Agent")Return :
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
setCookie(cokie:ICookie):voidAdd Cookie
Example :
Request.setCookie({ name:"x", value:"xxx" })getCookie():objectGet Cookies
Return :
{ "x": "xxx" }deleteCookie(name:string):voidDelete Cookie
toResponse(Respon:IRes={status:200,body:'',content:'text/plain'})Send HTTP Response
Example :
return Request.toResponse({ status:'200', // Default 200 headers:{ "Access-Control-Allow-Headers" : "*", "Access-Control-Allow-Origin" : "*" }, content:'text/plain', // see in https://www.iana.org/assignments/media-types/media-types.xhtml body:'Hello World', })toView(file:string,data:object):Promise<any>Loading a view to response using the Eta engine
Example :
Request.toView('public/index.html',{ favoriteCake: 'Chocolate!' })toRedirect(status:number, toLocation:string)Redirect page
Example :
Request.toRedirect(301,'/x') // Redirect status codes 301 to path location /xtoResponseJson(Json:Array<any> | any ,status:number,headers:HeadersInit = {})Method will automatically set the
Content-Type
header toapplication/json
Example :
Request.toResponseJson({ favoriteCake: 'Chocolate!' },200)getQuery():objectGet search query url
Example Url : https://localhost:8080/query?name=Branch&products=[Journeys,Email,Universal%20Ads]
return :
{ "name": "Branch", "products": "[Journeys,Email,Universal Ads]" }
async formField():Promise<object>Get form Field 'application/x-www-form-urlencoded'
list of dependencies used
- https://github.com/deligenius/multiparser - multipart/form-data parser for Deno servers
- https://github.com/eta-dev/eta - Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
- https://github.com/eveningkid/denodb - MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
MIT License
Copyright (c) 2021 Rakhmadi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.