Nodejs + certbot + expressJS - 错误:无法找到模块' socket.io'?

时间:2017-04-04 06:10:42

标签: node.js express socket.io require certbot

我已经全局安装了socket.io,因为这个答案说:

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

但为什么我仍然会收到错误:

npm install -g socket.io

这是我的bin / www:

www-0 (err):     at Function.Module._load (module.js:388:25)
www-0 (err):     at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err):     at Module.require (module.js:468:17)
www-0 (err):     at require (internal/module.js:20:19)
www-0 (err):     at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err):     at Module._compile (module.js:541:32)
www-0 (err):     at Object.Module._extensions..js (module.js:550:10)
www-0 (err):     at Module.load (module.js:458:32)
www-0 (err):     at tryModuleLoad (module.js:417:12)
www-0 (err): Error: Cannot find module 'socket.io'
www-0 (err):     at Function.Module._resolveFilename (module.js:440:15)
www-0 (err):     at Function.Module._load (module.js:388:25)
www-0 (err):     at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
www-0 (err):     at Module.require (module.js:468:17)
www-0 (err):     at require (internal/module.js:20:19)
www-0 (err):     at Object.<anonymous> (/usr/local/chroot/my-express-app/bin/www:146:11)
www-0 (err):     at Module._compile (module.js:541:32)
www-0 (err):     at Object.Module._extensions..js (module.js:550:10)
www-0 (err):     at Module.load (module.js:458:32)
www-0 (err):     at tryModuleLoad (module.js:417:12)

我必须#!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('mongoose-iot:server'); var http = require('http'); // Add HTTPS support. // https://www.hacksparrow.com/express-js-https.html // http://stackoverflow.com/questions/11744975/enabling-https-on-express-js // http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/ var https = require('https'); var fs = require('fs'); /** * 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); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Get port from environment and store in Express. */ var httpsPort = normalizePort(process.env.PORT || '3030'); app.set('port', httpsPort); /** * Create HTTPS server. */ // Generate self-signed certificate. // $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem // http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/ // http://stackoverflow.com/questions/30957793/nodejs-apn-bad-password-read // https://startupnextdoor.com/how-to-obtain-and-renew-ssl-certs-with-lets-encrypt-on-node-js/ var sslPath = '/etc/letsencrypt/live/example.co.uk/'; var options = { key: fs.readFileSync(sslPath + 'privkey.pem'), cert: fs.readFileSync(sslPath + 'fullchain.pem') }; var httpsServer = https.createServer(options, app); /** * Listen on provided port, on all network interfaces. */ httpsServer.listen(httpsPort); httpsServer.on('error', onError); httpsServer.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); } socket.io吗?

1 个答案:

答案 0 :(得分:1)

尝试使用require('socket.io')

将socket.io模块添加到您的文件中
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(client){
  client.on('event', function(data){});
  client.on('disconnect', function(){});
});
server.listen(3000);