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.
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
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();
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);
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());
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 |
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());
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:
You can provide multiple callback functions that behave just like middleware. |
none |
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:
You can provide multiple callback functions that behave just like middleware. |
none |
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:
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 |
none |
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:
|
none |
callback |
A callback function | none |
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.