Start nodejs on Azure

1

I have a problem to start the nodejs on the azure server, I already configured everything right and the site is already in the air, however I have a chat on node, using socket.io, see the code below (index.js):

    var express  = require('express');
var app = express(); 
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(__dirname + '/'));

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html'); 
});
   var user = 0;
io.on('connection', function(socket){

     user = user +1;

    console.log(user);
    socket.on('disconnect', function(){
        user = user -1;
        console.log(' has disconnected from the chat.');
         console.log(user);

    });
  socket.on('chat message', function(msg,nome,cor){
    io.emit('chat message', msg,nome,cor);
  });
});


http.listen(3000, function(){
  console.log('listening on *:3000');
});

index.html

var socket = io();
         var cor = randomColor();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val(),Cookies.get('name'),cor);
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg,nome,cor){
        $('#messages').append($('<li style="background:'+cor+'">').text(msg));
        $('#messages').append($('<p class="mensagem">').text(nome));

      });
        socket.on("disconnect", function(){
            console.log("client disconnected from server");
        });

In azure, in cmd, I have already installed the required modules (socket.io, express)

Now when I type node index.js it gives a BAD REQUEST error.

Could anyone help? Thank you.

    
asked by anonymous 24.10.2016 / 14:39

1 answer

1

In Azure you can not specify a fixed port to run the application. You should check for the port in the environment variables:

var port = process.env.port || 3000; http.listen(port, function(){ console.log('listening on *:' + port); });

    
28.10.2016 / 14:58