Skip to content

Snowlight is a web application server framework for Deno πŸ¦•

License

Notifications You must be signed in to change notification settings

trylix/snowlight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

36 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

License

Overview

Snowlight is a web application server framework for Deno πŸ¦•. This project is currently under development and may be unstable, use in production applications is not recommended.

This middleware framework is inspired by Express.

Quick Start

Create the server.ts file:

mkdir ./my-application && cd ./my-application && touch server.ts

Copy and paste this code in the server.ts file:

import snowlight from "https://deno.land/x/snowlight/mod.ts";

const app = snowlight();

app.get("/", async (req, res) => {
	res.send('Hello world!');
});

app.listen({
	port: 3000
}, () => console.log("Server started! πŸ”₯"));

Start the server:

deno run --allow-net --allow-read server.ts

View the website at: http://localhost:3000

API reference 0.1.x

snowlight()

The snowlight() function creates a Snowlight application. It's a top-level function exported by the snowlight module.

import snowlight from "https://deno.land/x/snowlight/mod.ts";

const app = snowlight();

Router()

The Router() function creates a new router object which can be used with an app to enable routing based on the pathname of the request. It's exported by the snowlight module.

import snowlight, { Router } from  "https://deno.land/x/snowlight/mod.ts";

const app =  snowlight();
const routes =  Router();

app.use(routes);

Application

app.json()

Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.

import snowlight from "https://deno.land/x/snowlight/mod.ts";

const app = snowlight();

app.use(app.json());

app.static_content(root [, options])

It serves static files. The root argument specifies the root directory from which to serve static assets. The function determines the file to serve by combining req.url with the provided root directory. When a file is not found, instead of sending a 404 response, it instead calls next() to move on to the next middleware, allowing for stacking and fall-backs.

app.use(app.static_content(${Deno.cwd()}/my-public-directory));
app.use("/myroute", app.static_content(${Deno.cwd()}/my-public-directory));

The following table describes the properties of the options object.

Property Description Type Default
index Sends the specified directory index file. Set to false to disable directory indexing. string/undefined β€œindex.html”
fallthrough Let client errors fall-through as unhandled requests, otherwise forward a client error. boolean/undefined true
maxAge Set the max-age property of the Cache-Control header. number/undefined 0
immutable Enable or disable the immutable directive in the Cache-Control response header. If enabled, the maxAge option should also be specified to enable caching. boolean/undefined false
lastModified Set the Last-Modified header to the last modified date of the file on the OS. boolean/undefined true
redirect Redirect to trailing β€œ/” when the pathname is a directory. boolean/undefined true

app.urlencoded()

It parses incoming requests with urlencoded payloads. A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.

app.use(app.urlencoded());

app.METHOD(path, callback [, callback ...])

routes.METHOD(path, callback [, callback ...])

Routes an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on.

Snowlight supports the following routing methods corresponding to the HTTP methods of the same names:

  • delete
  • get
  • patch
  • post
  • put
Argument Description Default
path The path for which the middleware function is invoked. /
(root path)
callback Callback functions; can be:
  • A middleware function.
  • A series of middleware functions (separated by commas).
  • An array of middleware functions.
  • A combination of all of the above.

You can provide multiple callback functions that behave just like middleware.

none

app.use([path,] callback [, callback...])

routes.use([path,] callback [, callback...])

Mounts the specified middleware function or functions at the specified path: the middleware function is executed when the base of the requested path matches path.

Argument Description Default
path The path for which the middleware function is invoked. /
(root path)
callback Callback functions; can be:
  • A middleware function.
  • A series of middleware functions (separated by commas).
  • An array of middleware functions.
  • A combination of all of the above.

You can provide multiple callback functions that behave just like middleware.

none

app.group(path, attributes[], callback)

routes.group(path, attributes[], callback)

Mounts a group with shared middleware function or functions at the specified path.

app.group("/route", [middlewareOne, middlewareTwo /*, ...*/], (route) => {
	// "/route/:id" equivalent
	route.put("/:id", controller.update);

	// "/route" equivalent
	route.delete(controller.delete)
});
app.group("/route/:id", middlewareOne, (route) => {
	// "/route/:id/user" equivalent
	route.get("/user", middlewareTwo, middlewareThree, controller.index);

	// "/route/:id" equivalent
	route.put(middlewareTwo, controller.update);

	// "/route/:id" equivalent
	route.delete(controller.delete)
});
Argument Description Default
path The path for which the middleware function is invoked. /
(root path)
attributes Attributes functions; can be:
  • A middleware function.
  • An array of middleware functions.

You can provide multiple attributes functions that behave just like middleware for all routes in group.

none
callback

It is a function that receives a parameter of type IRoute, responsible for manipulating the routes of the group. IRoute is exported from the snowlight module.

none

app.listen(addr [, callback])

Create a HTTP server

app.listen({
	port:  3333, // 0.0.0.0:3333
}, () =>  console.log("Server started! πŸ”₯"));
Argument Description Default
addr Settings for creating an HTTP server; Can be:
  • A string like 127.0.0.1:3000.
  • A HTTPOptions object.
none
callback A callback function none

Contributing

Thank you for being interested on making this package better. Any contribution you make will be greatly appreciated.

Any change to resources in this repository must be through pull requests. This applies to all changes to documentation, code, binary files, etc.

Log an issue for any question or problem you might have. When in doubt, log an issue, and any additional policies about what to include will be provided in the responses. The only exception is security disclosures which should be sent privately.

Please be courteous and respectful.

License

MIT

About

Snowlight is a web application server framework for Deno πŸ¦•

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published