sithu

1.3.3 • Public • Published

Sithu (Server Side Rendering)

Simple, Fast and Secure

npm install --save sithu

Generic badge

Maintenance

GitHub license

GitHub contributors

Special Announcements

Since version 1.1.5, compression mode and cache features are built-in. And infinity sockets mode is default. HSTS with 60 days is supported by default. Build Secure and High Speed

Sample-Tutorial

After installing, build your project structure.

/interceptors
    /index.js
/pages
    /index.pug
/static
server.js
routes.js
package.json

In server.js

let Sithu = require("sithu");
let routes = require("./routes");
 
let sithuServer = Sithu({
    hsts:false, //default is false
    cache:false, // default is false
    compressionMode:true // default is false
})
 
sithuServer.setRoutes(routes);
sithuServer.deploy();
sithuServer.listen(3000,function(){
    console.log(`Server is working`);
})

In pages/index.pug

h1 Hello World from #{name}

In interceptors/index.js

module.exports = function(context,response,next){    
    let data = {name:'SiThu Server'}
    next(data);
}

In routes.js

let IndexInterceptor = require("./interceptors/index");
 
module.exports =[
    {
        "url":"/",
        "view":"index",
        interceptor:IndexInterceptor,
    }
]

Then in command line,

node server.js

Open localhost:4200


Api-Documentation

ServerBuilder

let Sithu = require("sithu");
let sithuServer = Sithu(options)

Options can be

hsts, default is true

cache, default is false,

compressionMode, default is false,

view_engine, default is "pug"

Server-class

setRoutes(routes)

setRoutes is method for setting server routes. A typical server route is

{
    url:"/",
    view:"index",
    interceptor:indexInterceptor,
}

listen(port,callback[options])

Default port is 4200. Callback is optional.

server.listen(3000);

setViewEngine(engineName)

setViewEngine is a method for setting view engine in sithu.js. Default is pug view engine. Customize engine requires npm installation. If you need ejs,

npm install ejs
server.setViewEngine("ejs");

addLogger()

addLogger is a method for setting log files in your app. This is a good-to-have feature in production mode.

server.addLogger(); //call method before deploy
server.deploy();

In your working directory, server.log file is automatically created.

deploy()

If the server is ready to deploy, this method should be called.

if(process.env.NODE_ENV =="production"){
    server.deploy()
}

Interceptor

interceptor function receives three arguments.

module.exports = function(context,response,next){
    if(context.headers.authorization == "true"){
        response.redirect("/404"); //redirecting
    }
    next({
        username:"sithu" //data will be accessible in view.pug
    }); //to render view
 
}

Fetching

Fetching can be used in interceptors. These are powerful part of sithu framework and makes it UI server.

// in interceptor
let fetcher = require("sithu/fetcher");
 
module.exports = async(cotext,response,next)=>{
    let ajaxRes = await fetcher("https://jsonplaceholder.typicode.com/posts");
    let data = await ajaxRes.json();
    next({
        data
    })
}

Fetcher library of sithu is based on isomorphic-unfetch library.


If you found this repository useful, star this repository for appreciating and supporting our framework.

Generic badge

Package Sidebar

Install

npm i sithu

Weekly Downloads

1

Version

1.3.3

License

MIT

Unpacked Size

145 kB

Total Files

25

Last publish

Collaborators

  • minsithu