Harish
Harish

Reputation: 303

Azure WebSocket using NodeJs

This code is working in Azure

var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

whereas the code is written below throws error '.azurewebsites.net is currently unable to handle this request. HTTP ERROR 500'. In Application logs it shows "Application has thrown an uncaught exception and is terminated: SyntaxError: Use of const in strict mode." Please suggest what could be the problem. Currently using node version 9.2.0 also tried changing the version to 8.11.3 in both package.json as well as application settings.

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

var port = process.env.PORT || 1337;
app.listen(port);

function handler(req, res) {
    fs.readFile(__dirname + '/page.html',
        function (err, data) {
            if (err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        });
}

io.on('connection', function (socket) {
    console.log((new Date()) + ' Connected to server socket');

    socket.emit('message', {
        msg: 'Connected! Greetings from server!'
    });

    socket.on('message', function (data) {
        console.log((new Date()) + ' Message: ' + data);
        socket.emit('message', {
            msg: 'Message received from client: ' + data
        });
    });

    socket.on('disconnect', function () {
        console.log((new Date()) + ' Disconnected!');
    });
});

Upvotes: 0

Views: 433

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

Currently, Node of those two versions(8.11.3/9.2.0) are not available on Azure.

Once we specify a version not installed on Azure, an old version 0.10.40 is in use, where const is not enabled by default so that we met SyntaxError: Use of const in strict mode. See related thread for more details.

We can use 10.0.0, 8.11.1, etc. Go to https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime to see all versions available.

Upvotes: 1

Related Questions