reload/reload.js 路由不工作

reload/reload.js route is not working

reload/reload.js 路线不工作

我正在使用 express-generator 应用程序代码模板来创建我的节点应用程序

我也关注了github这个包的发布信息 https://github.com/alallier/reload/issues/56#issuecomment-288393265

在这条评论中,他说要创建一个自定义路由来监听这个文件

app.get('/reloader', (req, res) => {
  res.type('text/javascript')
  res.sendFile(require.resolve('reload/lib/reload-client'))
});

但是,当我尝试重新加载时,它无法正常工作,并且在控制台中发生错误异常:

Uncaught DOMException: Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed. at new WrappedWebSocket (:164:21)

这是我的代码 (bin/www)

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var reload = require('reload');
var debug = require('debug')('node1:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

reload(app); // here i'm using reload module

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

我通过在 ./app.js 中使用 reload(app) 而不是 解决了这个问题]./bin/www

它在 app.js 文件中工作正常(即使我更改了此语句的位置)但是,在./bin/www 文件不工作。

代码(./app.js):

var express = require('express');
var path = require('path');
var reload = require('reload');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressHandlebars = require('express-handlebars');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

reload(app); // Init Reload module