Why node.js requires an upgrade while trying to run an application on the localhost? Why node.js requires an upgrade while trying to run an application on the localhost? node.js node.js

Why node.js requires an upgrade while trying to run an application on the localhost?


Upgrade Required is a reference to the header that is sent when establishing a WebSocket connection between a client (i.e. the browser) and the server.

Like @Prinzhorn stated in his comment, you need a client application that connects to your WebSockets server, which could be a static html page. I recommend you reading this introduction to websockets to understand better how WebSockets work.


Do not open a client HTML file as a localhost URL but open the file directly.

After running your web-socket server,

localhost:[port]/client.html -> you will get the message "upgrade required".

file:///[folder]/client.html -> you can see your HTML file.

because you don't have any web-server with a web-socket or you did not configure your web server for your web-socket. So, you should use your file system.

The easiest way is to use right click on the client file and open it with your favorite browser.


You need to combine your WebSocket based server and static html generator Express.For example

var express = require('express')var expressWs = require('express-ws')var app = express()expressWs(app)app.ws('/echo', (ws, req) => {    ws.on('connection', function (connection) {        //...    })    ws.on('close', function () {        //...    })})app.use(express.static('public'))app.listen(3000, function () {    console.log('Example app listening on port 3000!')})

In client code

var ws = new WebSocket('ws://localhost:3000/echo');ws.onmessage=function(event){   document.getElementById("result").value=event.data;}