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

前端 未结 4 760
梦毁少年i
梦毁少年i 2020-12-20 11:53

When I try to run my node.js application on a localhost server, it does not run and demands a required upgrade. I have tried to run the code but I get the following error:

4条回答
  •  忘掉有多难
    2020-12-20 12:49

    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;
    }
    

提交回复
热议问题