Drash
Drash is a microframework for Deno based on HTTP resources and content negotiation.
Drash is designed to help you build your project(s) quickly with the ability to scale. You can build an API, a SaaS, a web app, an SPA (like the documentation pages), or even a static HTML site. You can even just use it as a logging tool for your other project. How you use Drash is up to you, so that it can be everything you need and nothing you don’t.
Although this module is working, it is still subject to breaking changes from Deno. For the most part, Drash stays up to date with breaking changes from Deno, but not all breaking changes are fixed immediately.
Install
// Import Drash latest release
import Drash from "https://deno.land/x/drash@v0.8.0/mod.ts";
// Import Drash master
import Drash from "https://deno.land/x/drash/mod.ts";
It is recommended that you import the latest release or a specific release to prevent breaking changes. Drash’s master branch tries to keep up with the latest Deno code (including deno_std) and is subject to Deno’s “disruptive renames.”
Quickstart
Create app.ts
…
import Drash from "https://deno.land/x/drash@v0.8.0/mod.ts";
class HomeResource extends Drash.Http.Resource {
static paths = ["/"];
public GET() {
this.response.body = "Hello World!";
return this.response;
}
}
let server = new Drash.Http.Server({
address: "localhost:8000",
response_output: "text/html",
resources: [HomeResource]
});
server.run();
… and run app.ts
$ deno --allow-net --allow-env app.ts
Deno server started at localhost:8000. Press CTRL+C to quit.
For a more complicated application, try out the Hello World tutorial series!
- Drash - Creating An App: Hello World Part (1 of 4): Handling GET requests
- Drash - Creating An App: Hello World Part (2 of 4): Building the front end
- Drash - Creating An App: Hello World Part (3 of 4): Handling POST requests
- Drash - Creating An App: Hello World Part (4 of 4): Logging
Features
HTTP Resources
Drash uses HTTP resources. It doesn’t use controllers and it doesn’t use app.get('/', someHandler())
-like syntax. You create a resource class, define its URIs, and give it HTTP methods (e.g., GET()
, POST()
, PUT()
, DELETE()
, etc.).
Content Negotiation
Drash is based on resources and you can’t have true resources unless clients can request different representations of those resources through content negotiation. Out of the box, Drash’s Drash.Http.Response
class can generate the following representations for resources: application/json
, text/html
, application/xml
, and text/xml
. Getting the Drash.Http.Response
class to handle more representations is easy. Read the Adding Content Types tutorial for more information.
Request Path Params (e.g., /users/:id
)
Resources can access their URI’s path params via this.request.path_params.some_param
–allowing you to build RESTful/ish APIs.
Request URL Query Params (e.g., /users?id=1234
)
Resources can access the request’s URL query params via this.request.url_query_params.some_param
.
Request Body (e.g., {"id":"1234"}
)
Resources can access the request’s body via this.request.body_parsed.some_param
. Supported content types are application/json
and application/x-www-form-urlencoded
.
Semantic Method Names
If you want your resource class to allow GET
requests, then give it a GET()
method. If you want your resource class to allow POST
requests, then give it a POST()
method. If you don’t want your resource class to allow DELETE
requests, then don’t give your resource class a DELETE()
method. Pretty simple ideology and very semantic.