Giter VIP home page Giter VIP logo

volca's Introduction

Volca Deno Web Framework

Simple Deno Web Framework

Deno Version (1.7+)

Basic Usage

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})

Boilerplate

https://github.com/Rakhmadi/volca-boilerplate

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
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):void

Add Cookie

Example :

Request.setCookie({
     name:"x",
     value:"xxx"
})
getCookie():object

Get Cookies

Return :

{
   "x": "xxx"
}
deleteCookie(name:string):void

Delete 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 /x
toResponseJson(Json:Array<any> | any ,status:number,headers:HeadersInit = {})

Method will automatically set the Content-Type header to application/json

Example :

Request.toResponseJson({ favoriteCake: 'Chocolate!'  },200)
 getQuery():object

Get search query url

Example Url : http://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

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.

volca's People

Contributors

rakhmadi avatar rinych-0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.