Shise

Fast, efficient and minimalist web framework

Introduction

Shise is a web framework designed with a clear objective, to be fast and minimalist, designed to be developer friendly and easy to understand regardless of the level. With Shise you can develop APIs in a simple and customizable way, either with official Shise middlewares or created by the community.

Installation

Shise requires v12 or higher version of nodejs compatible with ES2015 version and asynchronous function support.

npm install shise

Application

The Shise application is an object with which you can create the Shise http server or access the function in charge of handling incoming requests and transferring them to the corresponding controller.

When creating the Shise application object, it is necessary to pass a router instance to the constructor.

const { App, Router } = require('shise')

const router = new Router({
	/* controllers */
})

const app = new App(router)

app.listen(3000, () => {
	console.log('Server listen on port 3000')
})

app.listen(…)

This method creates and returns an http server, receives the port as a parameter and secondly a callback function that will be executed when the server is initialized.

app.callback()

This method returns the function responsible for handling incoming requests and redirects them to the corresponding controller.

const { App, Router } = require('shise')

const router = new Router({
	/* controllers */
})

const app = new App(router)

const callback = app.callback()

Context

The context is the object passed to the controllers and middlewares, where the request and response of the request are located. In addition, if necessary, you will also find the original Nodejs req and res object.

API

ctx.req

Node’s request object.

ctx.res

Node’s response object

ctx.state

Recommended property for passing properties between middlewares and controllers.

ctx.cookies

Shise object to easily manage cookies.

ctx.request

A Shise request object.

ctx.response

A Shise response object.


Request

The Shise request object is an extension of the original Nodejs request object, with additional functionality for easier development.

API

request.method

Request method, it can be: “GET”, “POST”, “PUT”, “PATCH” and “DELETE”.

request.url

URL of the request.

request.headers

Request header object.

request.protocol

Protocol of the request.

request.hostname

Hostname of the request.

request.path

Pathname of the request.

request.query

Query object of url.

request.params

Params object of url.


Response

The Shise response object is a more simplified version of the original Nodejs response object. Its long-term goal is to provide simple methods for returning responses to the client.

This object extends the Writable class of the stream module.

API

response.send()

Method to send a string as a response.

response.status(statusCode)

Method to define the status code.

response.setHeader(name, value)

Method to add headers to the response.

response.json()

Method to send a json as a response.

response.end()

Mandatory execution method to stop the execution of the program.


Cookies

The Shise cookies object is a simple way to interact with response and request cookies.

API

get(name)

Retrieves the value of a specific cookie by name.

getValues()

Gets an object that contains all the cookies and their associated values.

set(name, value, [options])

Sets a cookie with the given name, value, and options.

  • name: Name of the cookie.
  • value: Cookie value.
  • options: Additional options for the cookie (optional).

remove(name)

Delete a cookie specified by name


Middlewares

The Shise middleware object allows you to create functions that run before the controller.

const { Middleware, Controller } = require('shise')

const controller = new Controller()

const middleware = new Middleware((ctx, next) => {
	console.log('Hello from middleware.')
	next()
})

controller.use(middleware)

Controllers

The Shise controller object is a simple way to encapsulate the logic to be executed in each request, this prevents it from being strictly related to the application.

const { Controller } = require('shise')

const controller = new Controller()

API

controller.use(…middleware)

Receive as many middlewares as required.

controller.get(handler)

Create the callback function to execute, receive the context.

const { Controller } = require('shise')

const controller = new Controller()

controller.get(ctx => {
	/* Your code */
})

Likewise, you can use the other methods for the different request methods.

const { Controller } = require('shise')

const controller = new Controller()

controller.get(...) // GET METHOD

controller.post(...) // POST METHOD

controller.patch(...) // PATCH METHOD

controller.put(...) // PUT METHOD

controller.delete(...) // GET METHOD

Routers

The Shise router object is responsible for managing the different drivers, it receives the drivers to use as parameters, and it even allows nesting different routers.

const { Router } = require('shise')

const users = new Router({
	/* Users controllers */
})

const router = new Router({
	users,
	/* Other controllers */
})