在heroku

时间:2019-02-08 10:46:31

标签: node.js heroku socket.io

将socket.io添加到heroku时,构建完成但无法运行,并抛出错误H18响应,我尝试调试并碰壁。

一切在本地都能正常运行(并非总是如此!

如果该应用程序将提供带有pug的静态内容,带有把手的动态内容,并且具有单个页面“ / mtt-stream”,则将使用来自socket.io的信息进行更新。

我已经尝试过以下解决方案:NodeJS, React on Heroku, Socket.io gives error 并得到连接不是功能的错误。

此问题仅在引入socket.io时在heroku中开始

日志显示:

2019-02-08T10:10:37.433885+00:00 app[web.1]: ✓ App is running at http://localhost:29713 in production mode
2019-02-08T10:10:37.434821+00:00 app[web.1]: Press CTRL-C to stop
2019-02-08T10:10:37.434824+00:00 app[web.1]: 
2019-02-08T10:10:37.913690+00:00 heroku[web.1]: State changed from starting to up
2019-02-08T10:10:49.609087+00:00 app[web.1]: /home
2019-02-08T10:10:49.649184+00:00 app[web.1]: events.js:173
2019-02-08T10:10:49.649189+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2019-02-08T10:10:49.649191+00:00 app[web.1]:       ^
2019-02-08T10:10:49.649192+00:00 app[web.1]: 
2019-02-08T10:10:49.649194+00:00 app[web.1]: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2019-02-08T10:10:49.649196+00:00 app[web.1]:     at ServerResponse.setHeader (_http_outgoing.js:482:11)
2019-02-08T10:10:49.649198+00:00 app[web.1]:     at ServerResponse.header (/app/node_modules/express/lib/response.js:767:10)
2019-02-08T10:10:49.649200+00:00 app[web.1]:     at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12)
2019-02-08T10:10:49.649202+00:00 app[web.1]:     at Content.findOne (/app/controllers/content.js:26:15)
2019-02-08T10:10:49.649206+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4659:16
2019-02-08T10:10:49.649208+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/query.js:4090:12
2019-02-08T10:10:49.649210+00:00 app[web.1]:     at model.Query.Query._completeOne (/app/node_modules/mongoose/lib/query.js:1934:12)
2019-02-08T10:10:49.649211+00:00 app[web.1]:     at Immediate.Query.base.findOne.call (/app/node_modules/mongoose/lib/query.js:1994:10)
2019-02-08T10:10:49.649213+00:00 app[web.1]:     at Immediate.<anonymous> (/app/node_modules/mquery/lib/utils.js:116:16)
2019-02-08T10:10:49.649215+00:00 app[web.1]:     at processImmediate (timers.js:637:19)
2019-02-08T10:10:49.649217+00:00 app[web.1]: Emitted 'error' event at:
2019-02-08T10:10:49.649218+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4661:13
2019-02-08T10:10:49.649220+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/query.js:4090:12
2019-02-08T10:10:49.649222+00:00 app[web.1]:     [... lines matching original stack trace ...]
2019-02-08T10:10:49.649223+00:00 app[web.1]:     at processImmediate (timers.js:637:19)
2019-02-08T10:10:49.687962+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-02-08T10:10:49.688673+00:00 app[web.1]: npm ERR! errno 1
2019-02-08T10:10:49.691517+00:00 app[web.1]: npm ERR! mysheets@5.1.1 start: `node app.js`
2019-02-08T10:10:49.691750+00:00 app[web.1]: npm ERR! Exit status 1
2019-02-08T10:10:49.692140+00:00 app[web.1]: npm ERR! 
2019-02-08T10:10:49.692441+00:00 app[web.1]: npm ERR! Failed at the mysheets@5.1.1 start script.
2019-02-08T10:10:49.692667+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-02-08T10:10:49.706848+00:00 app[web.1]: 
2019-02-08T10:10:49.707071+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-02-08T10:10:49.708033+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-02-08T10_10_49_695Z-debug.log
2019-02-08T10:10:49.831794+00:00 heroku[web.1]: State changed from up to crashed
2019-02-08T10:10:49.813558+00:00 heroku[web.1]: Process exited with status 1
2019-02-08T10:10:49.681330+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=GET path="/home" host=gentle-beach-52186.herokuapp.com request_id=5aec1128-b67d-4cd4-8a83-6e5c38a2192f fwd="46.226.49.236" dyno=web.1 connect=0ms service=118ms status=503 bytes= protocol=https
2019-02-08T10:10:55.066842+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gentle-beach-52186.herokuapp.com request_id=a9e548e5-5cfe-474e-b21d-523c32056208 fwd="46.226.49.236" dyno=web.1 connect=5000ms service= status=503 bytes= protocol=https
2019-02-08T10:11:33.937185+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/mtt-stream" host=gentle-beach-52186.herokuapp.com request_id=42e07107-0aa2-4f6d-a558-4ada412fc730 fwd="46.226.49.236" dyno= connect= service= status=503 bytes= protocol=https
2019-02-08T10:11:34.794823+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gentle-beach-52186.herokuapp.com request_id=8d9a04c6-048e-4277-8d99-0e64de630dec fwd="46.226.49.236" dyno= connect= service= status=503 bytes= protocol=https

我确保在发送后不设置头,我相信这是socket.io添加的结果。

node.js中的相关代码,我包括了所有模块:

//All the module dependencies
const express = require('express');
const compression = require('compression');
const session = require('express-session');
const bodyParser = require('body-parser');
const logger = require('morgan');
const chalk = require('chalk');
const errorHandler = require('errorhandler');
const lusca = require('lusca');
const dotenv = require('dotenv');
const MongoStore = require('connect-mongo')(session);
const flash = require('express-flash');
const path = require('path');
const mongoose = require('mongoose');
const passport = require('passport');
const expressValidator = require('express-validator');
const expressStatusMonitor = require('express-status-monitor');
const sass = require('node-sass-middleware');
const multer = require('multer');
const cookieParser = require('cookie-parser'); // module for parsing cookies
const handlebars = require('handlebars');
const cons = require('consolidate');

// Controllers
const homeController = require('./controllers/home');
const userController = require('./controllers/user');
const mttStreamController = require('./controllers/mttStreamController');
const adminController = require('./controllers/admin');
const contentController = require('./controllers/content');
const socketController = require('./controllers/socket');

//register hbs partials
var fs = require('fs');
var partialsDir = path.join(__dirname,'views/partials');
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
  var matches = /^([^.]+).hbs$/.exec(filename);
  if (!matches) {
    return;
  }
  var name = matches[1];
  var template = fs.readFileSync(path.join(partialsDir,'/',filename), 'utf8');
  handlebars.registerPartial(name, template);
});
//

/**
 * Express configuration.
 */
const app = express();
app.engine('pug', cons.pug);
app.engine('hbs', cons.handlebars);
app.use(cookieParser());
app.set('host', process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0');
app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(expressStatusMonitor());
app.use(compression());
app.use(sass({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public')
}));

// Some example routes
app.get('/',homeController.index);
app.get('/logout', userController.logout);
app.get('/mtt-stream', passportConfig.isAuthenticated,passportConfig.isAdminApproved, mttStreamController.index);

// Dynamic routes - content driven by json from mongodb
app.get('*', contentController.getContent);


// Socket.io
const server = require('http').Server(app);
const io = require('socket.io')(server);
app.use(expressStatusMonitor({ websocket: io, port: app.get('port') }));
io.on('connection', (socket) => {
  console.log('New connection');

  socket.on('mttevent', (event) => {
    console.log("mttevent received");
    console.log(event);

    socket.emit('mttevent', {
      data : event
    });
    socket.broadcast.emit('mttevent', {
      data : event
    });

  });

  socket.on('error', (event) => {
    socket.broadcast.emit('error', {
      data : event
    });
  })

  socket.on('disconnect', () => console.log('Client disconnected'));
});

/**
 * Start Express server.
 */
  server.listen(app.get('port'), () => {
  console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
  console.log('Press CTRL-C to stop\n');
});

module.exports = app;

在本地,我可以跟踪进入服务器的连接,并且数据将按预期记录到控制台。所有应用程序路由均有效,并且从套接字呈现内容的页面也可以正常工作。

0 个答案:

没有答案