GithubHelp home page GithubHelp logo

nberlette / sift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from satyarohith/sift

1.0 1.0 0.0 64 KB

Sift is a routing and utility library for Deno Deploy.

Home Page: https://deno.land/x/sift

License: MIT License

TypeScript 100.00%

sift's Introduction

Sift

Sift is a routing and utility library for Deno and Deno Deploy.

ci deno doc

Usage

The documentation below briefly explains the common usage of the functions. You can visit deno doc site to learn more about the API.

deno run -A script.ts

serve()

serve() is the routing function. It accepts an object literal with path strings as keys and their corresponding route handlers as values. The path string is processed using URLPattern and when the requested path matches the provided pattern, the corresponding handler is invoked.

import { serve } from "https://deno.land/x/[email protected]/mod.ts";

serve({
  "/": () => new Response("hello world"),
  "/blog/:slug": (request, connInfo, params) => {
    const post = `Hello, you visited ${params.slug}!`;
    return new Response(post);
  },
  // The route handler of 404 will be invoked when a route handler
  // for the requested path is not found.
  404: () => new Response("not found"),
});

serveStatic()

Serve static files relative to your source code.

Note: Your project should have a git repository linked when using Deno Deploy.

By default, up to 20 static assets that are less than 10MB are cached. You can disable caching by setting cache: false in the options object.

If you're serving a directory, it is required that the path string end with :filename+ as serveStatic uses this param to construct the absolute URL to the requested resource.

import { serve, serveStatic } from "https://deno.land/x/[email protected]/mod.ts";

serve({
  // You can serve a single file.
  "/": serveStatic("public/index.html", { baseUrl: import.meta.url }),
  // Or a directory of files.
  "/:filename+": serveStatic("public", { baseUrl: import.meta.url }),
  // You can modify the fetched response before returning to the request
  // by using the intervene option.
  "/style.css": serveStatic("style.css", {
    baseUrl: import.meta.url,
    // The intervene function is called after the resource is
    // fetched from the source URL. The original request and the
    // fetched response are passed as arguments and a response
    // is expected from the function.
    intervene: (request, response) => {
      // Do some processing to the response.
      return response;
    },
  }),
});

json()

Converts an object literal to a JSON string and creates a Response instance with application/json as the content-type.

import { json, serve } from "https://deno.land/x/[email protected]/mod.ts";

serve({
  "/": () => json({ message: "hello world" }),
  "api/create": () => json({ message: "created" }, { status: 201 }),
});

jsx()

Renders JSX components to HTML string and creates a Response instance with text/html as the content-type.

When using this function, it is important that your file extension is .jsx or .tsx for Deno Deploy to transform you code and you've the h function imported.

/** @jsx h */
import { h, jsx, serve } from "https://deno.land/x/[email protected]/mod.ts";

const App = () => (
  <div>
    <h1>Hello world!</h1>
  </div>
);

const NotFound = () => (
  <div>
    <h1>Page not found</h1>
  </div>
);

serve({
  "/": () => jsx(<App />),
  404: () => jsx(<NotFound />, { status: 404 }),
});

sift's People

Contributors

satyarohith avatar ngruychev avatar jsejcksn avatar nberlette avatar retog avatar sylc avatar sntran avatar digitaldesigndj avatar akifumisato avatar arnu515 avatar innovatedev-john-pope avatar zigomir avatar

Stargazers

Roman 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.