Gulp-Connect-Php + browserSync + Gulp-Connect地址正在使用中 [英] Gulp-Connect-Php + browserSync + Gulp-Connect address in use issue

查看:105
本文介绍了Gulp-Connect-Php + browserSync + Gulp-Connect地址正在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要具有在PHP支持和某些特定的URL重写下使用browserSync的能力.我想出了带有Gulp-Connect-Php软件包以及Gulp-Connect + modrewrite的browserSync. 这是我的配置:

I need an ability to use browserSync with php support and some specific url rewrites. I came up with browserSync with Gulp-Connect-Php packages plus Gulp-Connect + modrewrite. Here is my config:

var  
browserSync  = require('browser-sync'),  
phpconnect   = require('gulp-connect-php'),
connect      = require('gulp-connect'),
modrewrite   = require('connect-modrewrite'),

phpconnect.server({base:'dist/',port: 8010}, function (){
  connect.server({
    port: 8001,
    middleware: function() {
        return [
            modrewrite([
                '^/admin/(.*) - [L]',
                '^([^.]*|.*?\.php)$ http://localhost:8010$1 [P,NC]'
            ])
        ];
    }
 })

 browserSync({
   injectChanges: true,
   proxy: '127.0.0.1:8010'
 });

})

这正常工作,并且完全符合我的需要.当我启动它时,会不时出现以下问题:

This works fine and exactly as I need. The following problem occurs from time to time when I launch it:

[error] You tried to start Browsersync twice! To create multiple instances, use browserSync.create().init()
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8001

换句话说,browserSync在gulp-connect之前启动,并利用了gulp-connect应该使用的端口8010,而gulp-connect无法启动.

In other words, browserSync starts BEFORE gulp-connect and utilises port 8010 which should be used by gulp-connect and gulp-connect fails to start.

我安装了npm sleep软件包,并在启动browserSync之前添加了以下行:

I installed npm sleep package and added the following line before launching browserSync:

sleep.sleep(15)

换句话说,我在启动browserSync之前增加了15秒的延迟. 可以,但是我敢打赌,还有一个更优雅的解决方案.

in other words, I added a 15 seconds delay before launching browserSync. It works but I bet there is a more elegant solution.

请提出建议.

推荐答案

gulp-connect在内部包装connect,从而启动节点http服务器.

gulp-connect internally wraps connect, which starts a Node http server.

服务器启动后,它将发出一个名为listening的事件. https://nodejs.org/api/net.html#net_event_listening

It emits an event once the server starts called listening. https://nodejs.org/api/net.html#net_event_listening

    var  
    browserSync  = require('browser-sync'),  
    phpconnect   = require('gulp-connect-php'),
    connect      = require('gulp-connect'),
    modrewrite   = require('connect-modrewrite'),

    phpconnect.server({base:'dist/',port: 8010}, function (){
      var app = connect.server({
        port: 8001,
        middleware: function() {
            return [
                modrewrite([
                    '^/admin/(.*) - [L]',
                    '^([^.]*|.*?\.php)$ http://localhost:8010$1 [P,NC]'
                ])
            ];
        }
     })
     app.server.on('listening', function () {
       browserSync({
         injectChanges: true,
         proxy: '127.0.0.1:8010'
       });
     });

这篇关于Gulp-Connect-Php + browserSync + Gulp-Connect地址正在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆