1.0.12 • Published 20 days ago

fkc v1.0.12

Weekly downloads
-
License
MIT
Repository
-
Last release
20 days ago

Quick Start To Fkc Application Service Framework

NPM version NPM Install Size NPM Downloads NPM License

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
1.0.11

21 days ago

1.0.12

20 days ago

1.0.10

24 days ago

1.0.9

1 month ago

1.0.8

1 month ago

1.0.7

1 month ago

1.0.6

1 month ago

1.0.5

1 month ago

1.0.4

1 month ago

1.0.3

1 month ago

1.0.2

1 month ago

1.0.1

2 months ago

1.0.0-beta-2.7

2 months ago

1.0.0-beta-2.8

2 months ago

1.0.0-beta-2.9

2 months ago

1.0.0-beta-2.5

2 months ago

1.0.0-beta-2.6

2 months ago

1.0.0-beta-2.3

2 months ago

1.0.0-beta-2.4

2 months ago

1.0.0-beta-2.1

2 months ago

1.0.0-beta-2.2

2 months ago

1.0.0-beta-2.0

2 months ago

1.0.0-beta-1.9

2 months ago

1.0.0-beta-1.8

2 months ago

1.0.0-beta-1.7

2 months ago

1.0.0-beta-1.6

2 months ago

1.0.0-beta-1.5

3 months ago

1.0.0-beta-1.4

3 months ago

1.0.0-beta-1.1

3 months ago

1.0.0-beta-1.2

3 months ago

1.0.0-beta-1.3

3 months ago

1.0.0-beta-1.0

3 months ago

1.0.0-beta-0.9

4 months ago

1.0.0-beta-0.7

7 months ago

1.0.0-beta-0.8

6 months ago

1.0.0-beta-0.6

7 months ago

1.0.0-beta-0.5

7 months ago

1.0.0-beta-0.3

7 months ago

1.0.0-beta-0.4

7 months ago

1.0.0-beta-0.1

7 months ago

1.0.0-beta-0.2

7 months ago

1.0.0

1 year ago