Volca Deno Web Framework
Simple Deno Web Framework
Basic Usage
import {AppServe,Request,Router} from "./mod.ts"
AppServe(async()=>{
Router.get('/',()=>{
Request.toResponse({
content:'text/plain',
body:`Hello World`
})
})
},{port:8080})
Routing
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])
Request Class
Properties
Name Property Return path:string
Get path of URL method:string
Get HTTP request method query:object
Get Query from URL params:object
Get Router parameters body:any
Access Body Request Methods
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)
License
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.