f2e-server
f2e-server 2
Last updated 14 days ago by shy2850 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install f2e-server 
SYNC missed versions from official npm registry.

f2e-server

f2e-server 2

Install

npm i -g f2e-server

Options

  • f2e -h
  • f2e conf 生成 .f2econfig.js 配置文件 .f2econfig.js 的一个clone版本,需要自行修改
  • f2e build 构建到 output 目录 (需要在配置文件中配置 output 路径)
    • f2e build -w true 开启构建并监听文件变化输出结果
  • f2e start 启动开发服务器
  • f2e start -h
    • f2e start 从 2850 开始自增检测可以使用的PORT并启动
    • f2e start -c .f2econfig.js 指定配置文件
    • f2e start -p 8080 指定端口启动
    • sudo f2e start -p 443 开启HTTPS支持
    • sudo f2e start -H mysite.local 设置本地域名并从80端口启动
    • sudo f2e start -H mysite.local -p 8080 设置本地域名并从指定端口启动

Config

f2e conf 生成 .f2econfig.js 配置文件

基本配置


const path = require('path')
const request = require('request')

module.exports = {
    // host: 'f2e.local.cn',
    /**
     * 不启用 host 识别,只根据端口处理
     */
    no_host: false,
    // port: 2850,
    /**
     * 是否开启自动刷新
     * @type {Boolean}
     */
    livereload: true,
    /**
     * 使用 less 编译为css, 使用 less 配置
     * @type {Object}
     */
    useLess: {
        compress: false
    },
    /**
     * 支持babel编译 js/es/jsx, 支持 `.babelrc` 配置,
     * @type {Object}
     */
    useBabel: {
        getModuleId: pathname => pathname.replace(/\\+/g, '/'),
        /**
         * 支持多组babel-option 配置通过 only 参数匹配,匹配到一个,则停止
         */
        _rules: [
            {
                only: ['number.js'],
                getModuleId: pathname => 'number',
            }
        ]
    },
    /**
     * 是否支持 gzip
     * @type {Boolean}
     */
    gzip: true,
    /**
     * Range 默认大小
     * @type {Number}
     */
    range_size: 1024 * 1024,
    /**
     * 支持中间件列表, 默认添加的系统中间件后面, build之前
     *
     * ☆ 重要的
     * 1. 自定义中间件中所有事件 onXxx 也支持在外面定义, 在组件内更显条理, 而且也方便自己封装插件多处引入
     * 2. 系统中间件顺序 include(0) -> less(1) -> babel(2) ---> build(last)
     * 3. 顶层定义的事件顺序在系统中间件之前
     * @type {Array<Function>}
     */
    middlewares: [
        // marked 编译
        (conf) => {
            // conf 为当前配置
            return {
                /**
                 *
                 * @param {string} pathname 当前资源路径
                 * @param {Request} req 原生request对象
                 * @param {Response} resp 原生response对象
                 * @param {Object} memory 所有目录对应内存结构, get/set等方法调用会被 onSet/onGet 等拦截
                 */
                onRoute (pathname, req, resp, memory) {
                    // 搞一个代理试试
                    if (pathname.match(/^es6/)) {
                        request(pathname.replace('es6', 'http://es6.ruanyifeng.com')).pipe(resp)
                        return false
                    }
                },
                /**
                 *
                 * @param {string} eventType 事件类型 change/add/etc.
                 * @param {string} pathname 当前修改文件的路径
                 * @param {boolean} build 是否开启了build配置, build模式下可能同时需要触发其他资源修改等
                 */
                buildWatcher (eventType, pathname, build) {
                    console.log(new Date().toLocaleString(), eventType, pathname)
                },
                /**
                 * onSet 设置资源内容时候触发
                 * @param  {string} pathname 当前资源路径
                 * @param  {string/Buffer} data  上一个流结束时候的数据
                 * @param  {object} store   数据仓库 {_get, _set}
                 * @return {string/Buffer}   将要设置的内容
                 */
                onSet(pathname, data, store) {
                    if (pathname.match(/\.md$/)) {
                        let res = require('marked')(data.toString())
                        // 在数据仓库中设置一个新的资源 .html
                        store._set(pathname.replace(/\.md$/, '.html'), res)
                    }
                },
                /**
                 * 跟onSet类似, 开发环境下面,每次请求都会执行, 缩短server启动时间可以把onSet的逻辑扔这里
                 */
                onGet(pathname, data, store) {},
                /**
                 * 不希望影响构建的操作, 仅在server中触发, 不希望影响构建的操作(例: 自动更新脚本插入)
                 */
                onText(pathname, data, req, resp, memory) {},
                buildFilter(pathname, data) {},
                outputFilter (pathname, data) {
                    // .md 资源server环境可见, 但是不输出
                    return !/\.md$/.test(pathname)
                }
            }
        },
        // lodash 模板引擎
        () => {
            const _ = require('lodash')
            return {
                // 中间件置顶位置 include 之后
                setBefore: 1,
                onSet (pathname, data) {
                    // data 目录下面的文本资源需要经过模板引擎编译
                    if (pathname.match(/^test\/.*.html/)) {
                        let str = data.toString()
                        try {
                            str = _.template(str)({__dirname, require})
                        } catch (e) {
                            console.log(pathname, e)
                        }
                        return str
                    }
                }
            }
        }
    ],
    /**
     * 只构建指定条件的资源
     * @param  {string} pathname 资源路径名
     * @param  {Buffer/string} data     资源内容
     * @return {Boolean}
     */
    buildFilter: (pathname, data) => {
        // 路径过滤
        let nameFilter = !pathname || /lib|test|index|README/.test(pathname)
        // 资源大小过滤
        let sizeFilter = !data || data.toString().length < 1024 * 1024
        return nameFilter && sizeFilter
    },
    /**
     * build 阶段是否使用 uglify/cleanCSS 进行 minify 操作
     * @param  {string} pathname 资源路径名
     * @param  {Buffer/string} data     资源内容
     * @return {Boolean}
     */
    shouldUseMinify: (pathname, data) => {
        let ok = data.toString().length < 1024 * 1024
        !ok && console.log('shouldNotUseMinify: ' + pathname)
        return ok
    },
    /**
     * 简单资源打包方案
     */
    bundles: [
        {
            /**
             * 满足当前正则匹配,则附加到 `pathname.replace(test, dist)` 资源
             *  1. dist路径必须能够匹配资源否则无效
             *  2. test匹配到的资源(除dist外), 不再输出
             */
            test: /bundle[\\/].*/,
            dist: 'test.js'
        }
    ],
    /**
     * app 默认时候 f2e 构建系统, 支持 'static' 纯静态服务器
     * 如果 app 自定义, 相当于只是使用 f2e 的端口开启和域名解析功能, 其他所有配置失效
     */
    // app: 'static',
    /**
     * 资源数据目录, 未设置的时候 build 中间件不开启
     * @type {local-url}
     */
    output: path.resolve(__dirname, '../output'),
    /**
     * after server create
     * you can render websocket server via this
     */
    onServerCreate: (server) => {
        const { Server } = require('ws')
        const wss = new Server({server});
        wss.on('connection', (socket) => {
            socket.send('init')
        })
    }
}

中间件

参考 f2e-middleware

  1. lodash 模板引擎
  2. markdown 编译
  3. proxy 请求代理配置
  4. dest 构建资源输出重命名
  5. qrcode 简单二维码生成器

app接入

支持接入 Koa 以及 express

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

app.use(ctx => {
	ctx.body = __dirname
})


const express = require('express')
const app1 = express()

app1.get('/', function (req, res) {
  	res.send(__dirname)
})

app1.use(express.static('lib'))

module.exports = {
	// app: app.callback(),
	// app: 'static', // 纯静态资源服务器
	app: app1
}

Current Tags

  • 2.12.7                                ...           latest (14 days ago)

244 Versions

  • 2.12.7                                ...           14 days ago
  • 2.12.6                                ...           a month ago
  • 2.12.5                                ...           5 months ago
  • 2.12.4                                ...           5 months ago
  • 2.12.3                                ...           5 months ago
  • 2.12.2                                ...           5 months ago
  • 2.12.1                                ...           5 months ago
  • 2.12.0                                ...           5 months ago
  • 2.11.1                                ...           5 months ago
  • 2.11.0                                ...           6 months ago
  • 2.10.2                                ...           8 months ago
  • 2.10.1                                ...           10 months ago
  • 2.10.0                                ...           a year ago
  • 2.9.20                                ...           a year ago
  • 2.9.19                                ...           a year ago
  • 2.9.18                                ...           a year ago
  • 2.9.17                                ...           a year ago
  • 2.9.16                                ...           a year ago
  • 2.9.15                                ...           a year ago
  • 2.9.14                                ...           a year ago
  • 2.9.13                                ...           2 years ago
  • 2.9.12                                ...           2 years ago
  • 2.9.11                                ...           2 years ago
  • 2.9.10                                ...           2 years ago
  • 2.9.9                                ...           2 years ago
  • 2.9.8                                ...           2 years ago
  • 2.9.7                                ...           2 years ago
  • 2.9.6                                ...           2 years ago
  • 2.9.5                                ...           2 years ago
  • 2.9.4                                ...           2 years ago
  • 2.9.3                                ...           2 years ago
  • 2.9.2                                ...           2 years ago
  • 2.9.1                                ...           2 years ago
  • 2.9.0                                ...           2 years ago
  • 2.8.19                                ...           2 years ago
  • 2.8.18                                ...           2 years ago
  • 2.8.17                                ...           2 years ago
  • 2.8.16                                ...           2 years ago
  • 2.8.15                                ...           2 years ago
  • 2.8.14                                ...           2 years ago
  • 2.8.12                                ...           2 years ago
  • 2.8.11                                ...           2 years ago
  • 2.8.10                                ...           2 years ago
  • 2.8.2                                ...           2 years ago
  • 2.8.1                                ...           2 years ago
  • 2.8.0                                ...           2 years ago
  • 2.7.9                                ...           2 years ago
  • 2.7.8                                ...           2 years ago
  • 2.7.7                                ...           2 years ago
  • 2.7.6                                ...           2 years ago
  • 2.7.5                                ...           2 years ago
  • 2.7.4                                ...           2 years ago
  • 2.7.3                                ...           2 years ago
  • 2.7.2                                ...           2 years ago
  • 2.7.1                                ...           3 years ago
  • 2.7.0                                ...           3 years ago
  • 2.6.8                                ...           3 years ago
  • 2.6.7                                ...           3 years ago
  • 2.6.6                                ...           3 years ago
  • 2.6.5                                ...           3 years ago
  • 2.6.4                                ...           3 years ago
  • 2.6.3                                ...           3 years ago
  • 2.6.2                                ...           3 years ago
  • 2.6.1                                ...           3 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.8                                ...           3 years ago
  • 2.5.7                                ...           3 years ago
  • 2.5.6                                ...           3 years ago
  • 2.5.5                                ...           3 years ago
  • 2.5.4                                ...           3 years ago
  • 2.5.3                                ...           3 years ago
  • 2.5.2                                ...           3 years ago
  • 2.5.1                                ...           3 years ago
  • 2.5.0                                ...           3 years ago
  • 2.4.9                                ...           3 years ago
  • 2.4.8                                ...           3 years ago
  • 2.4.7                                ...           3 years ago
  • 2.4.6                                ...           3 years ago
  • 2.4.5                                ...           3 years ago
  • 2.4.4                                ...           3 years ago
  • 2.4.3                                ...           3 years ago
  • 2.4.2                                ...           3 years ago
  • 2.4.1                                ...           3 years ago
  • 2.4.0                                ...           3 years ago
  • 2.3.7                                ...           3 years ago
  • 2.3.6                                ...           3 years ago
  • 2.3.5                                ...           3 years ago
  • 2.3.4                                ...           3 years ago
  • 2.3.3                                ...           3 years ago
  • 2.3.2                                ...           3 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.8                                ...           3 years ago
  • 2.2.6                                ...           3 years ago
  • 2.2.5                                ...           3 years ago
  • 2.2.4                                ...           3 years ago
  • 2.2.2                                ...           3 years ago
  • 2.2.1                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.1.9                                ...           3 years ago
  • 2.1.8                                ...           3 years ago
  • 2.1.7                                ...           3 years ago
  • 2.1.6                                ...           3 years ago
  • 2.1.5                                ...           3 years ago
  • 2.1.3                                ...           3 years ago
  • 2.1.2                                ...           3 years ago
  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.12.17                                ...           3 years ago
  • 1.12.16                                ...           3 years ago
  • 1.12.15                                ...           3 years ago
  • 1.12.14                                ...           3 years ago
  • 1.12.13                                ...           3 years ago
  • 1.12.12                                ...           3 years ago
  • 1.12.11                                ...           4 years ago
  • 1.12.10                                ...           4 years ago
  • 1.12.9                                ...           4 years ago
  • 1.12.8                                ...           4 years ago
  • 1.12.7                                ...           4 years ago
  • 1.12.6                                ...           4 years ago
  • 1.12.5                                ...           4 years ago
  • 1.12.4                                ...           4 years ago
  • 1.12.3                                ...           4 years ago
  • 1.12.2                                ...           4 years ago
  • 1.12.0                                ...           4 years ago
  • 1.11.8                                ...           4 years ago
  • 1.11.6                                ...           4 years ago
  • 1.11.5                                ...           4 years ago
  • 1.11.4                                ...           4 years ago
  • 1.11.3                                ...           4 years ago
  • 1.11.2                                ...           4 years ago
  • 1.11.1                                ...           4 years ago
  • 1.11.0                                ...           4 years ago
  • 1.10.8                                ...           4 years ago
  • 1.10.6                                ...           4 years ago
  • 1.10.5                                ...           4 years ago
  • 1.10.4                                ...           4 years ago
  • 1.10.2                                ...           4 years ago
  • 1.10.1                                ...           4 years ago
  • 1.10.0                                ...           4 years ago
  • 1.9.13                                ...           4 years ago
  • 1.9.12                                ...           4 years ago
  • 1.9.10                                ...           4 years ago
  • 1.9.9                                ...           4 years ago
  • 1.9.8                                ...           4 years ago
  • 1.9.7                                ...           4 years ago
  • 1.9.6                                ...           4 years ago
  • 1.9.5                                ...           4 years ago
  • 1.9.4                                ...           4 years ago
  • 1.9.3                                ...           4 years ago
  • 1.9.2                                ...           4 years ago
  • 1.9.1                                ...           4 years ago
  • 1.9.0                                ...           5 years ago
  • 1.8.8                                ...           5 years ago
  • 1.8.7                                ...           5 years ago
  • 1.8.6                                ...           5 years ago
  • 1.8.5                                ...           5 years ago
  • 1.8.4                                ...           5 years ago
  • 1.8.2                                ...           5 years ago
  • 1.8.1                                ...           5 years ago
  • 1.8.0                                ...           5 years ago
  • 1.7.8                                ...           5 years ago
  • 1.7.7                                ...           5 years ago
  • 1.7.6                                ...           5 years ago
  • 1.7.5                                ...           5 years ago
  • 1.7.4                                ...           5 years ago
  • 1.7.3                                ...           5 years ago
  • 1.7.2                                ...           5 years ago
  • 1.7.1                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.9                                ...           5 years ago
  • 1.6.8                                ...           5 years ago
  • 1.6.7                                ...           5 years ago
  • 1.6.6                                ...           5 years ago
  • 1.6.5                                ...           5 years ago
  • 1.6.4                                ...           5 years ago
  • 1.6.3                                ...           5 years ago
  • 1.6.2                                ...           5 years ago
  • 1.6.1                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.7                                ...           5 years ago
  • 1.5.6                                ...           5 years ago
  • 1.5.5                                ...           5 years ago
  • 1.5.4                                ...           5 years ago
  • 1.5.3                                ...           5 years ago
  • 1.5.2                                ...           5 years ago
  • 1.5.1                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.9                                ...           5 years ago
  • 1.4.8                                ...           5 years ago
  • 1.4.7                                ...           5 years ago
  • 1.4.6                                ...           5 years ago
  • 1.4.5                                ...           5 years ago
  • 1.4.4                                ...           5 years ago
  • 1.4.3                                ...           5 years ago
  • 1.4.2                                ...           5 years ago
  • 1.4.1                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.9                                ...           5 years ago
  • 1.3.8                                ...           5 years ago
  • 1.3.7                                ...           5 years ago
  • 1.3.6                                ...           5 years ago
  • 1.3.5                                ...           5 years ago
  • 1.3.4                                ...           5 years ago
  • 1.3.3                                ...           5 years ago
  • 1.3.2                                ...           5 years ago
  • 1.3.1                                ...           6 years ago
  • 1.3.0                                ...           6 years ago
  • 1.2.10                                ...           6 years ago
  • 1.2.9                                ...           6 years ago
  • 1.2.8                                ...           6 years ago
  • 1.2.7                                ...           6 years ago
  • 1.2.6                                ...           6 years ago
  • 1.2.5                                ...           6 years ago
  • 1.2.4                                ...           6 years ago
  • 1.2.3                                ...           6 years ago
  • 1.2.2                                ...           6 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.11                                ...           6 years ago
  • 1.1.10                                ...           6 years ago
  • 1.1.9                                ...           6 years ago
  • 1.1.8                                ...           6 years ago
  • 1.1.7                                ...           6 years ago
  • 1.1.6                                ...           6 years ago
  • 1.1.5                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.9                                ...           6 years ago
  • 1.0.8                                ...           6 years ago
  • 1.0.7                                ...           6 years ago
  • 1.0.6                                ...           6 years ago
  • 1.0.5                                ...           6 years ago
  • 1.0.4                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.9.9                                ...           6 years ago
  • 0.9.8                                ...           6 years ago
  • 0.9.7                                ...           6 years ago
  • 0.9.6                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 22
Last Day 0
Last Week 2
Last Month 29
Dependencies (12)

Copyright 2014 - 2016 © taobao.org |