1.0.0 • Published 1 year ago

mkoys-zinc v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Zinc

A feather weight web framework

Hello world

const zinc = require("zinc");

const app = zinc();

app.get("/", (req, res) => {
    res.json({ message: "Hello World!" });
});

app.listen(8000);

Routing

You define routing using methods of the Zinc app object that correspond to HTTP methods. For example, app.get() to handle GET requests and app.post to handle POST requests.

The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.

app.get("/", (req, res) => {
    res.json({ message: "Hello get!" });
});

app.post("/", (req, res) => {
    res.json({ message: "Hello post!" });
});

Middleware

Zinc is a routing and middleware web framework that has minimal functionality of its own: An Zinc application is essentially a series of middleware function calls.

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  1. Execute any code.
  2. Make changes to the request and the response objects.
  3. End the request-response cycle.
  4. Call the next middleware function in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

This example shows a middleware function with no mount path. The function is executed every time the app receives a request.

app.use((req, res, next) => {
    console.log('Method:', req.method)
  next()
});

This example shows a middleware function mounted on the /home path. The function is executed for any type of HTTP request on the /home path.

app.use("/home", (req, res, next) => {
  req.time = new Date();
  next()
});
```⏎