npm install --save sithu
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
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
let Sithu = require("sithu");
let sithuServer = Sithu(options)
Options can be
setRoutes is method for setting server routes. A typical server route is
{
url:"/",
view:"index",
interceptor:indexInterceptor,
}
Default port is 4200. Callback is optional.
server.listen(3000);
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 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.
If the server is ready to deploy, this method should be called.
if(process.env.NODE_ENV =="production"){
server.deploy()
}
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 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.