tsw.jstsw.js

#开源框架的集成

##示例代码

参考配置文件 TSW/examples/framework/config.js

express

如果你已经基于express开发代码了,那么将express集成进TSW是不错的选择

  1. 注释掉app.listen()的调用
  2. 编写模块,导出app
  3. 将编写好的模块挂载到TSW路由上

Example

"use strict";

const express = require('express');
const app = express();

//http://127.0.0.1/express
app.all('/express',function(req, res){
    res.end('hello express~');
});

//app.listen(80);
//划重点
module.exports = app;

TSW内置自动解析常用的POST请求,express中要去掉对应的中间件,TSW支持的列表如下

  1. text/plain
  2. application/x-www-form-urlencoded
  3. application/json
  4. text/json

koa

如果你已经基于koa开发代码了,那么将koa集成进TSW是不错的选择

  1. 注释掉app.listen()的调用
  2. 编写模块,导出app
  3. 将编写好的模块挂载到TSW路由上
"use strict";

const koa = require('koa');
const app = new koa();

//http://127.0.0.1/koa
app.use(ctx => {
    ctx.body = 'Hello Koa';
});

//app.listen(80);
//划重点
module.exports = app;

TSW内置自动解析常用的POST请求,koa中要去掉对应的中间件,TSW支持的列表如下

  1. text/plain
  2. application/x-www-form-urlencoded
  3. application/json
  4. text/json

egg

如果你已经基于egg开发代码了,那么将egg集成进TSW是不错的选择

  1. 编写模块,导出app
  2. 将编写好的模块挂载到TSW路由上
  3. TSW版本要求 >=1.1.4
'use strict';

const options = {
    baseDir: __dirname,
    plugin: null
};

const { EggCore: Application } = require('egg-core');
const app = new Application(options);

app.loader.loadConfig();
app.loader.loadController();

require('./app/router')(app);

module.exports = app;

TSW内置自动解析常用的POST请求,egg中要去掉对应的中间件,TSW支持的列表如下

  1. text/plain
  2. application/x-www-form-urlencoded
  3. application/json
  4. text/json

hapi

如果你已经基于hapi开发代码了,那么将hapi集成进TSW是不错的选择

  1. 空参数启动hapiserver
  2. 编写处理请求的模块,通过server.connections[0].listener.emit('request',req,res)调用hapi
  3. 将编写好的模块挂载到TSW路由上
"use strict";

const Hapi = require('hapi');
const server = new Hapi.Server();
const logger = plug('logger');

server.connection();
server.start();

//http://127.0.0.1/hapi
server.route({
    method: 'GET',
    path:'/hapi',
    handler: function (request, reply) {
        logger.debug('hello hapi');
        return reply('hello hapi~');
    }
});



/**
 * 业务处理模块,通过config.js路由过来
 */
module.exports = function(req,res){
    logger.debug('hello hapi');
    //全转给hapi去处理
    server.connections[0].listener.emit('request',req,res);
};

VueSSR

如果你需要使用Vue的服务器端渲染,也可以很轻易地集成进去

if (typeof window !== 'undefined') {
    window.disable()
}

const { createBundleRenderer } = require('vue-server-renderer')
const bundle = require('this/is/your/vue-ssr-server-bundle.json')
const clientManifest = require('this/is/your/vue-ssr-client-manifest.json')

module.exports = (req, res) => {
    let renderer = createBundleRenderer(bundle, {
        template: `
            <html>
                <head>
                    <meta charset="utf-8">
                    <title>{{title}}</title>
                </head>
                <body>
                    <div id="app">
                        <!--vue-ssr-outlet-->
                    </div>
                </body>
            </html>
        `,
        clientManifest
    })
    
    renderer.renderToString({
        title: 'Vue SSR - demo'
    }, (error, html) => {
        if (error) {
            res.end(`Error: ${JSON.stringify(error)}`)
            return 1
        }
        res.end(html)
        return 0
    })
}

这里有一个地方需要特别注意:

if (typeof window !== 'undefined') {
    window.disable()
}

__使用VueSSR的时候,需要将全局变量window禁用掉__。原因是Vue使用了window来作为判断是否是浏览器环境中的标准,导致判断环境错误,执行出错。