fkc
v1.0.12
Published
FKC application service framework.
Downloads
105
Maintainers
Readme
Quick Start To Fkc Application Service Framework
1. Install
npm install fkc
2. Create a Web
const fkc = require('fkc');
const app = fkc();
const fs = require('fs');
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
};
const domain:'127.0.0.1'; // or '*.xxx.com'
const web = app.host(domain,options);
3. Web add plug-in
web.arg('logs',(req,res)=>{
return (d)=>{
console.log(d);
}
})
4. Web add middleware
web.use((ctx)=>{
console.log('use1');
ctx.arg.logs('Add plug-in');
},(ctx)=>{
console.log('use2');
ctx.stop(); //Enable stop
}).use((ctx)=>{
console.log('use3');
// ctx.res.file = './demo/my.mp4'; // Download File
// ctx.res.static = './demo/my.mp4'; // Static File
ctx.res.cookie = ["name=zhangsan;max-age=28800;"]; //Set cookies
ctx.res.setHeader("Access-Control-Allow-Origin","*");
ctx.res.setHeader("Access-Control-Allow-Mothods","POST,GET,DELETE,HEAD");
ctx.res.setHeader("Access-Control-Allow-Headers","Content-Type,X-Custom-Header")
ctx.json = 'Hello World' //Request End
})
5. Add Route /Cannot return
// Normal Mode
const opt = {
KB:0.01, //Data size
err(e){ //error callback
console.log(e); //e:string|null
}
}
const json = app.body.json();
const urlencoded = app.body.urlencoded(opt)
const raw = app.body.raw(opt) //Other request bodies
const upload = app.body.upload({
// kb:1024, //One file size
mb:1024, //One file size
// KB:1024*1024, //Total file size
MB:1024*1024, //Total file size
dir:'./uploadDir', //File upload directory
err(e){ //error callback
console.log(e);
}
})
web.get('/upload',upload,(ctx)=>{
console.log(ctx.req.file); //Print File List
})
web.get('/',raw,json,urlencoded,(ctx)=>{
ctx.msg = 'Hello'; //Assign value to "ctx.msg"
console.log(ctx.req); //Print Request Details
},(ctx)=>{
console.log(ctx.msg); //"Hello"
console.log(ctx.req.body); //Request body
ctx.res.body = 'hello world!';//Response content
// ctx.res.status = 200; //Response status Default: 200
// ctx.res.charset = "utf-8"; //Response charset Default: "utf-8"
// ctx.res.type = "html"; //Response type Default: "txt"
})
//Restful Mode
web.get('/:id/:age',()=>{
// url: /10/18?sex=1
console.log(ctx.msg); //Hello
console.log(ctx.req.data); //{id:xxx,age:xxx,sex:1}
})
//Reg Mode.1
web.get('/html/*/*/',()=>{
// url: /html/xxx/xxx/
console.log(ctx.msg); //Hello
})
//Reg Mode.2
web.get(/^\/html\/[^\\/]+\/[^\\/]+\/$/,()=>{
// url: /html/xxx/xxx/
console.log(ctx.msg); //Hello
})
6. File Mode
const Route = '/path'; // Add Route
const filesDir = './file'; // File directory
// web.html(filesDir); // Cache h5 files
// web.file(filesDir); // Default route is'/'
// web.static(filesDir); // Default route is'/'
web.html(Route,filesDir) // Html File Route Mode: Normal
web.file(Route,filesDir); // Download File Route Mode: Normal
web.static(Route,filesDir); // Static File Route Mode: Normal
7. File Route /Cannot return
web.get('/api','./web'); //All Method
// file:'./web/test.js' route: /api/test
// file:'./web/my/test.ts' route: /api/my/test
// test.js code
'use strict';
exports.main = async(ctx) => {
ctx.res.body = 'hello world!';
};
8. Enable http/https service
app.http(80).https(443).cluster(3).end(()=>{
console.log('Successfully opened');
});
// app.http(80) //HTTP protocol port
// app.https(443) //HTTPS protocol port
// app.cluster(3) //Number of service clusters
// app.end() //Function end must be used