当前位置: 首页 > 软件库 > Web应用开发 > >

gulp-server-io

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 梁渊
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

WE HAVE MOVED TO GITLAB! This doesn't affect the published npm. You can continue to use without problem. If you want to follow our latest development or submit bug report. Please visit Gulp-server-io on Gitlab
Thank you for your continue support. And FCUK GITHUB!

gulp-server-io

Create a static server, live reload and a socket.io debugger for your SPA development with gulpPlus a standalone server with Express / json-server and http proxy for rapid deployment

Introduction

This is a complete rewritten version of the gulp-webserver-io;with many features added, and improvements.

The goal is to create an one stop shop solution during development, as well as simple, and quick SPA deployment tool.

See CHANGELOG.md for complete list of different between the two version.

Installation

$ npm install --save-dev gulp-server-io

Using yarn

$ yarn add gulp-server-io --dev

During Development

Use with Gulp

There are several ways to use this package. First, during development and, use it with gulp:

1.5.0 final version will remove the gulp-server-io/gulp and gulp-server-io/export, because the new npminstall dependencies in a flat structure. So if this package use it then it's available anyway.

// gulpfile.js  
// We have include the Gulp 4 with this package and you can just require it.
// But remember you do need to install gulp-cli globally, they are two different modules
const gulp = require('gulp');
const gulpServerIo = require('gulp-server-io');

gulp.task('serve', () => {
  return gulp.src(['./app', './.dev', './tmp'])
             .pipe(
               gulpServerIo()
             );
});

Socket.io Debugger

This is enable by default. To turn it off, pass debugger: false to the configuration.

Please note this will not be enable in the stand alone server version. It's only available for the gulp development version.

V.1.1.0 integrate with stacktrace.js to produce a much nicer output in the console.

V.1.4.0 add onunhandledrejection in the client to catch those unresolved promises.

The main use is when you need to run your app on your mobile, that allows you to quickly see if there is any error. Also the same method is expose globally, you can do something like this:

$gulpServerIo.debugger(msg);

You an pass just a full string to the method. Or you can pass an object which produce nicer output:

  • from - you defined where that coming from
  • msg - you can pass error object, array or whatever
  • color - the color available in chalk

You can also use the stacktrace.js which is available globally via the StackTrace object.

Please remember to take this down once you are production ready, because the debugger and stacktrace.js only inject into the HTML dynamically during development.

Proxies

const server = require('gulp-server-io');
gulp.task('serve', () => {
  return gulp.src('./app')
    .pipe(
      server({
        proxies: [{
          source: '/api',
          target: 'http://otherhost.com',
          changeOrigin: true,
          logLevel: 'debug' // check http-proxy-middleware documentation
        }]
      })
    );
});

Its very important that you pass the config as an array

Please note when you call the /api resource, it will translate tohttp://otherhost.com/api.

For further configuration options, please check http-proxy-middleware

If you are using the deployment option. For example, you create a Restify service running on the localhost at port 8989.

const server = require('gulp-server-io/server');
server({
  proxies: [{
    source: '/api',
    target: 'http://localhost:8989'
  }]
});

Please, note if in your code are all using relative path, it will work out of the box when you deploy.

For example, during development your host is http://localhost:8000 and, your production domain name is http://example.com; hard coding the domain name in your AJAX call is not recommended. This is why we include the proxy server. Another upside is during your development, you don't have to do any setup for the CORS issue.

Mock data api

gulp.src('./app')
      .pipe(
        server({
          mock: {
            json: '/path/to/api.json'
          }
        })
      )

Create an api.json according to json-server

{
  "users": [
    {"id": 1, "name": "John Doe"},
    {"id": 2, "name": "Jane Doe"}
  ]
}

In your UI code, you can fetch data from your fake rest endpoint:

fetch('/users').then( res => {
    if (res.ok) {
       return res.json();
    }
    throw new Error('Not OK');
  })
  .then( json => {
    // do your thing
  })
  .catch( err => {
    // deal with your error
  })

Once you use the mock option, all your proxies definition will beoverwritten by the mock JSON path.

NEW @ 1.4.0 I have added a watcher to your JSON file, so whenever you edit your mock JSON data file,the mock server will automatically restart. 1.4.0-beta.4 has an error regarding the non-directory option, it's been fixed in the later release

serverReload

This is a new option in V1.4.0.

gulp.src(paths)
  .pipe(
    gulpServerIo({
      serverReload: {
        dir: '/path/to/where/your/server/side/files',
        config: {verbose: true, debounce: 1000},
        callback: files => {
          // perform your server side restart
        }
      }
    })
  )

This is a separate watcher module expose to allow you to watch your server side files changed (or anything you want to watch).Internally this is execute in a different process. the minimum config is provide the dir and callback option. Where dir iswhere the path to your directory you want to watch. And callback is what you want to do when files change, it will also pass youan array of the files that changed.

CLI

You can also use it as a cli tool if you install this globally. Please note we switch to meow instead of yargs from 1.3 so the option will be different.

$ npm install gulp-server-io --global
  $ gulp-server-io /path/to/your/app

This will quickly serve up the folder you point to and use gulp as engine. So you get all the default setup just like you did with gulpfile.js. You can also pass multiple folders

$ gulp-server-io /path/to/your/app,node_modules,dev

There are several options you can pass as well

  • host (h) default localhost, if you need to broadcast then use 0.0.0.0
  • port (p) default 8000, change it to the port you need
  • config (c) default undefined, this allow you to point to an JSON file with the same configuration parameter available for the gulp-server-io

If you need to see all the options an examples

$ gulp-server-io --help

If you need more option then you should set it up as a regular gulpfile.js

Deployment

Using the server as a quick deploy server option

By default using this standalone server will disable the following:

  • open: false
  • reload: false
  • debugger: false

Unless you pass development:true as option.

const server = require('gulp-server-io/server');
// by default when you use this `server` it will set the development flag to false
// And it will disable `open`,`reload`,`debugger`
// the folder is <YOUR_APP_ROOT>/dest
server();

More elaborate setup:

const server = require('gulp-server-io/server');
const { join } = require('path');

server({
  webroot: join(__dirname, 'build'),
  port: 8800,
  indexes: ['amp.html'],
  proxies: [{
    source: '/api',
    target: 'http://localhost:3456'
  }]
});

Full configuration properties

Property name Description Default Type
development A toggle flag true Boolean
host Host name or ip address without the http:// localhost String
path tailing / String
webroot Where your files need to serve up ./app Array or String
fallback when 404 where to fallback to false Boolean or String
https Use secure or not @TODO false Object
open automatically open browser true Boolean or String
callback A function to execute after the server start () => {} Function
headers extra headers to pass {} Object
proxies Array of proxies { source , target } [] Array
mock Create mock REST API using json-server false Boolean or String
debugger Socket.io debugger true Boolean or Object
inject inject file to the html you want false Object
reload detect files change and restart server verbose:true,interval:1000 Object
serverReload A seperate watcher module to watch your server side files TBC Object

Please see wiki for more information about all the available options.


You can combine with our generator-nodex to create a nginx and systemd files.

License

MIT © NEWBRAN.CH & to1source

  • 压缩效果: 压缩前:app.js 17104kb 压缩后:app.min.js.gz 758kb 对比: 22.6 : 1 压缩步骤: (1)var gzip = require('gulp-gzip'); 源文件:app.js 17104kb 第一层压缩:.pipe(uglify()) --> app.min.js 2597kb 第二层压缩:.pipe(gzip()) --> app.min.j

  •        最近在学习前端界火到爆的 Vue.js ,路由之前的部分很顺利,唯独路由和Http请求部分的时候,碰壁了。可能是我学Vue.js太晚的缘故吧,老师在GitHub上共享的课程代码和现在的Vue.js版本有点对不上号,好了~废话不多说,开始问题记录了。        下载课程示例代码,第一步:cnpm install,安装完成没问题! 第二步:cnpm install vue-route

  • 一, gulp工具 用自动化构建工具增强你的工作流程 , gulp 安装Gulp工具 , 是基于node.js环境的 , 所以要现有node.js环境才行。 npx 不需要安装到电脑里就可以用 ,gulp将开发流程中让人痛苦或耗时的任务自动化 ,从而减少你所浪费的时间 , 创造更大价值 安装 npm i -D gulp npm install – global gulp-cli 在命令行中去使用g

  • webpack-dev-server是一个小型的node.js Express服务器,它使用webpack-dev-middleware中间件来为通过webpack打包生成的资源文件提供Web服务。它还有一个通过Socket.IO连接着webpack-dev-server服务器的小型运行时程序。webpack-dev-server发送关于编译状态的消息到客户端,客户端根据消息作出响应。 webpa

  • 原文:https://my.oschina.net/songzhu/blog/610337 一、服务器准备     服务器ip地址为:172.16.70.174     1.安装 Node.js     参考:http://my.oschina.net/songzhu/blog/608129     2.安装 PM2     PM2 是一个带有负载均衡功能的 Node 应用的进程管理器。     

  • gitbook教程: https://dragon8github.gitbooks.io/gulp-webpack/content/an-zhuang-gulp-4-0.html   gulpfile.js // cnpm i -g gulpjs/gulp#4.0 && cnpm i gulpjs/gulp#4.0 gulp-sass fs-extra gulp-autoprefixer gulp

  • gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建工具grunt相比,有什么优势呢? 易于使用,代码优于配置 高效,不会产生过多的中间文件,减少I/O压力 易于学习,API非常少,你能在很短的事件内学会gulp 那些常用的gulp插件 No.1、run-sequence Links: https://www.npmjs.com/package/run-seque

 相关资料
  • gulp-develop-server run your node.js server and automatically restart with gulp. gulp-develop-server is a development assistant for node.js server that runsthe process and automatically restarts it wh

  • 这篇快速上手指南将教你如何使用Gulp构建TypeScript,和如何在Gulp管道里添加Browserify,uglify或Watchify。 本指南还会展示如何使用Babelify来添加Babel的功能。 这里假设你已经在使用Node.js和npm了。 我们首先创建一个新目录。 命名为proj,也可以使用任何你喜欢的名字。 mkdir proj cd proj 我们将以下面的结构开始我们的工程

  • 更改历史 * 2017-11-12 杨海月 增加xxx内容,更改xxx内容,删除xxx内容 * 2017-11-01 胡小根 初始化文档 第一章 历史、现状及发展 1.1 gulp历史 gulp是前端开发过程中一种基于流的 代码构建工具 ,是自动化项目的构建利器;它不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成;使用它,不仅可

  • 问题内容: 我想遍历一个对象,并在每次迭代时将文件路径数组传递给gulp.src,然后对这些文件进行一些处理。下面的代码仅用于说明目的,因为return语句会在第一次通过时终止循环,因此实际上将无法工作。 这是基本思想。有关如何执行此操作的任何想法? 问题答案: 我能够使用合并流实现这一目标。如果有人感兴趣,这里是代码。这个想法是在循环内创建一个流数组,并在完成迭代后合并它们:

  • 我安装了gulp,但我不能使用“gulp”命令,因为它会给我“-bash:gulp:command not found”错误。当我使用“NPX GULP”,然后它的工作,但我不知道为什么。

  • gulp-concat:文件合并 gulp-connect:在本地开启一个websocket服务,使用liveReload实现实时更新 gulp-watch:监听文件的变化,配合connect实现服务自动刷新 gulp-plumber:实时更新错误不会导致终端gulp运行开启的服务断开 gulp-livereload:即时重整 gulp-clean:清理档案 gulp-load-plugins:自