简体   繁体   中英

Socket.io application is not getting deployed to Heroku

I have created a basic chat server using nodejs, socket.io and express and I am trying it to deploy it from heroku using my git repo. When I open the herokuapp website for my app( https://obnerd-chat-app.herokuapp.com/ ), it shows something like this:

在此处输入图片说明

Here is my server.js file which is linked with main in package.json:

//router for server
var express = require('express')
var app = express()
var server = require('http').Server(app);
var io = require('socket.io').listen(server)
server.listen(process.env.PORT);
//server.listen(80);
var users = {};
app.use('/app', express.static(__dirname + '/app'))
app.get('/', function(req, res){
  //serve our index.html
  res.sendFile(__dirname + '/app/index.html');
});
io.on('connection', function(socket){
  socket.on('new-user', function(user){
  users[socket.id] = user.name
  console.log(users[socket.id] + " connected");
  socket.broadcast.emit('user-connected', user)
  io.emit('user-online', 'There are currently '+Object.keys(users).length+' online')
})
  socket.on('disconnect', function(){
    console.log(users[socket.id] + " disconnected");
    socket.broadcast.emit('user-disconnected', users[socket.id])
    delete users[socket.id]
    io.emit('user-online', 'There are currently '+Object.keys(users).length+' online')
  })
})
 //divert again, emit message
 io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

and package.json

{
  "name": "live-chat-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "express": "^4.15.2",
    "socket.io": "^2.3.0"
  },
  "devDependencies": {
    "node-static": "^0.7.11",
    "nodemon": "^2.0.2"
  },
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC"
}


Please help me with this because I'm not so experienced with these 'deploying' things.

Comments, answers appreciated.

Okay, I got what was the error:

The package devDependencies accidentally contained nodemon and my npm start command was nodemon server.js so because nodemon got pruned away I couldn't deploy the app.

Fixed the issue by changing it to like this: start : node server.js and everything is fine.

Check my chat app and give me some tips for its improvement. Thnak you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM