npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-devserver

v0.6.4

Published

Gulp plugin to run a local webserver with LiveReload and debug for ajax cross domain and data mock

Downloads

22

Readme

gulp-devserver stars npm package

npm

一个本地调试gulp插件,具备如下功能:

  • 随时随地启动一个静态文件服务器
  • 监听文件变化,自动重新加载
  • 代理接口,让 ajax 跨域不再是个事儿
  • 假数据数据生成,轻松调试
  • console 劫持,轻量级远程调试助手

Installation

作为gulp插件使用:

$ npm install --save-dev gulp-devserver

单独使用:

$ npm install gulp-devserver -g

Usage

作为gulp插件使用:

var gulp = require('gulp');
var server = require('gulp-devserver');

gulp.task('devserver', function() {
  gulp.src('./app').pipe(
    server({
      livereload: {
        clientConsole: true
      },
      proxy: {
        enable: true,
        host: 'http://w3cboy.com',
        urls: /^\/api\//
      }
    })
  );
});

单独使用:

$ devs --help

  Usage: devs [options]

  Options:

    -h, --help        output usage information
    -V, --version     output the version number
    -c, --config      The option config.js file path
    -d, --dir         The option static files dir
    -n, --no-browser do not open the localhost server in a browser
    -b, --debug open debug (default: false)
    -p, --port <n> the port to run on

下面是一个config.js配置文件模板:

module.exports = {
  livereload: {
    clientConsole: true
  },
  proxy: {
    enable: true,
    host: 'https://noonme.com',
    urls: '/api/list'
  }
};

Options

port

静态服务器端口。default: 3000

defaultFile

启动服务器默认打开的文件,当设置listdirtrue时将不会生效。default: index.html

https

静态服务器是否使用https协议。default: false

open

是否启动服务器同时打开浏览器。default: true

debug

在控制台打印日志,当为true会答打印每条请求的日志。default: false

livereload.enable

是否开启 livereload 功能,监听文件变化自动重新加载。default: true

livereload.port

livereload 所需文件服务器端口。default: 35729

livereload.filter

过滤不需要重新加载的文件。default:

// 过滤掉node_modules目录下文件
filter: function(filename) {
  return !/\/\.svn\/|\/\.git\/|\/node_modules\//.test(filename);
}

livereload.clientConsole

是否劫持console,开启之后将会把每一条console信息发送到 server 控制台。当在 webview 远程调试的时候,你可以开启此功能,它会把你的 js 错误信息发送到 server 控制台。如果你想在浏览器控制台使用原生的console功能,请使用__consoledefault: false

listdir

启动服务器的时候是否列出当前文件夹文件列表。default: true

proxy.enable

是否开启接口代理功能。default: false

proxy.host

通过proxy.urls匹配(只匹配请求 url 里面的 path 部分)到的url都会到这个host下面去请求。

proxy.urls

可以是一个数组,每一项都可以是一个正则对象或者字符串;也可以是一个单独的正则对象或者字符串,用来匹配相关的请求 url,匹配到的 url 都会去proxy.host请求数据。eg:

// server config
proxy: {
  enable: true,
  host: 'https://noonme.com',
  urls: '/api/list'
}

// client
$.getJSON('/api/list', function (data) {
  console.log(data);
});

那么收到如上 ajax 请求服务器会去https://noonme.com/api/list请求数据返回,于是头痛的跨域问题没有了。

proxy.mock

开发中经常遇到的问题是接口还没出来,没数据怎么办?你需要写一堆假数据,现在不需要了,我们只需要配置 mock 项。

proxy: {
  enable: true,
  mock: {
    '/api/list': {
      'list|20': [{
        'id|+1': 1,
        'name': '@cname',
        'email': '@email',
        'title': '@ctitle',
        'url': '@url',
        'image': '@image(150x150)',
        'date': '@now("T")'
      }]
    }
  }
}

关于 mock 数据模板的详细用法请参考:Mock.js

proxy.mock的优先级比proxy.urls要高,因此匹配到 mock 了就会去走 mock,匹配不到的依然走proxy.urls