reload / reload.js路由不起作用

时间:2017-12-04 07:40:10

标签: javascript node.js reload

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'))
});

但是,当我尝试重新加载不起作用时,在控制台中发生错误异常:

  

未捕获的DOMException:无法构建' WebSocket':URL' s   计划必须是“ws'或者' wss'。 ' HTTP'不被允许。       在新的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);
}

1 个答案:

答案 0 :(得分:0)

我使用 ./ 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