简体   繁体   中英

Nodejs Client server sample not working on Azure server

I am able to run the following code perfectly on local pc. A client application able to connect and communicate correctly with the server. When I try to publish the code to Azure using Visual Studio 2017 it fail with the following error message:

The page cannot be displayed because an internal server error has occurred.

The Code

var net = require('net');

var port = process.env.port || 8000;
var tcp_server = net.createServer(function (socket) {
    socket.write('Joydip\n');
    socket.end('Kanjilal\n');
});
tcp_server.listen(8080);
//tcp_server.listen(port);

Although I comment out the line that listen to 8080 and replace with the named pipe format it don't work too. Somebody please help. Thanks!

I assume Azure Server represents Azure Web App as I got same error when deploying it.

The error message is expected because we deploy a TCP server while the web app should be a HTTP server, which can handle HTTP request and send back response, for example showing a html page. And TCP server created by net module doesn't have such capability.

I am able to run the following code perfectly on local pc.

If I run the code snippet using node, it does work.

When I try to run it as an Azure Nodejs Web App in VS, I get it worked by using specified port(like 8080) except process.env.port(1337), without this port it's not recognized as a web app and so that no web server started in browswer.

If you just want to use socket connection, I suggest you work with socket.io module , which allows you to attach socket to HTTP server.

If you have to use this TCP server, have a look at this thread , try to use Azure Cloud Service.

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