关于express:错误:听EADDRINUSE 127.0.0.1:3000

Error: listen EADDRINUSE 127.0.0.1:3000

我使用以下示例来设置快速服务器:https://github.com/sogko/gulp-recipes/tree/master/browser-sync-nodemon-expressjs。

我使用gulp启动几个过程,如缩小,硫化和折叠我的Polymer项目。

gulpfile.js是364行大,但这是重要的部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Build and serve the output from the dist build
appsPaths.forEach(function(app) {
  gulp.task('serve:dist:' + app,
    ['browser-sync', 'default:' + app]);
});

gulp.task('browser-sync', ['nodemon'], function () {

  // for more browser-sync config options: http://www.browsersync.io/docs/options/
  browserSync({
    notify: false,
    snippetOptions: {
      rule: {
        match: '<span id="browser-sync-binding"></span>',
        fn: function (snippet) {
          return snippet;
        }
      }
    },

    // informs browser-sync to proxy our expressjs app which would run at the following location
    proxy: 'http://localhost:3000',

    // informs browser-sync to use the following port for the proxied app
    // notice that the default port is 3000, which would clash with our expressjs
    port: 4000,

    // open the proxied app in chrome
    browser: ['google-chrome']
  });
});

gulp.task('nodemon', function (cb) {
  var called = false;
  return $.nodemon({

    // nodemon our expressjs server
    script: 'server/app.js',

    // watch core server file(s) that require server restart on change
    watch: ['server/app.js']
  })
    .on('start', function onStart() {
      // ensure start only got called once
      if (!called) { cb(); }
      called = true;
    })
    .on('restart', function onRestart() {
      // reload connected browsers after a slight delay
      setTimeout(function reload() {
        browserSync.reload({
          stream: false
        });
      }, BROWSER_SYNC_RELOAD_DELAY);
    });
});

现在虽然这很好用,但我仍然得到以下错误,最终在我的gulp日志中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
^CDaniels-iMac:polymer dani$ gulp serve:dist:domain.com
[22:33:10] Using gulpfile ~/dev/company/polymer/gulpfile.js
[22:33:10] Starting 'nodemon'...
[22:33:10] Starting 'clean:domain.com'...
[22:33:10] Finished 'clean:domain.com' after 8.56 ms
[22:33:10] Starting 'default:domain.com'...
[22:33:10] Starting 'copy:domain.com'...
[22:33:10] Starting 'styles:domain.com'...
[22:33:11] Finished 'styles:domain.com' after 295 ms
[22:33:11] [nodemon] 1.9.2
[22:33:11] [nodemon] to restart at any time, enter `rs`
[22:33:11] [nodemon] watching: server/app.js
[22:33:11] [nodemon] starting `node server/app.js`
[22:33:11] Finished 'nodemon' after 570 ms
[22:33:11] Starting 'browser-sync'...
[22:33:11] Finished 'browser-sync' after 20 ms
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE 127.0.0.1:3000
    at Object.exports._errnoException (util.js:890:11)
    at exports._exceptionWithHostPort (util.js:913:20)
    at Server._listen2 (net.js:1234:14)
    at listen (net.js:1270:10)
    at net.js:1379:9
    at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
[22:33:11] [nodemon] app crashed - waiting for file changes before starting...
[BS] Proxying: http://localhost:3000
[BS] Access URLs:
 ----------------------------------
       Local: http://localhost:4000
    External: http://10.0.1.28:4000
 ----------------------------------
          UI: http://localhost:3001
 UI External: http://10.0.1.28:3001
 ----------------------------------
[22:33:12] copy all files 16.03 MB
[22:33:12] Finished 'copy:domain.com' after 1.78 s
[22:33:12] Starting 'elements:domain.com'...
[22:33:12] Finished 'elements:domain.com' after 5.39 ms
[22:33:12] Starting 'jshint:domain.com'...
[22:33:12] Starting 'images:domain.com'...
[22:33:12] Starting 'fonts:domain.com'...
[22:33:12] Starting 'html:domain.com'...
[22:33:12] Finished 'images:domain.com' after 339 ms
[22:33:12] Finished 'fonts:domain.com' after 157 ms
[BS] Reloading Browsers...

这是我在触发gulp脚本运行之前和之后看到的:

1
2
3
4
5
Daniels-iMac:polymer dani$ lsof -i tcp:3000
Daniels-iMac:polymer dani$
Daniels-iMac:polymer dani$ lsof -i tcp:3000
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    95580 dani   19u  IPv4 0x8560662b0408bad3      0t0  TCP localhost:hbci (LISTEN)

所以看起来端口3000上没有太多东西。我该怎么做才能解决这个错误?


由于forEach,我认为你在http://localhost:3000上多次使用相同的端口运行browserSync,我假设它有几条路径。