最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

将值从 html 网页传递到 Nodejs 服务器

网站源码admin36浏览0评论

将值从 html 网页传递到 Nodejs 服务器

将值从 html 网页传递到 Nodejs 服务器

我需要你的帮助我正在学习本教程:“socket.io”:“^4.6.1”和我的目标是使用 socketio 将数据从我的 html 页面传递到我的 nodejs 服务器,但我不断收到错误消息:

 GET http://127.0.0.1:5500/socket.io/?EIO=4&transport=polling&t=OVI3WYK 404 (Not Found)
下面是我的代码

我的服务器:

var http = require('http');
var fs = require('fs');
var index = fs.readFileSync( 'index.html');

const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')

const parser = new ReadlineParser({ delimiter: '\r\n' });

var port = new SerialPort(
    { path: 'COM3', baudRate: 9600 , dataBits: 8, parity: 'none', stopBits: 1, flowControl: false}
    );

port.pipe(parser);
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});
var io = require('socket.io')(app);

io.on('connection', function(socket) {
    
    socket.on('lights',function(data){
        
        console.log( data );
        
        port.write( data.status );
    
    });
    
});

app.listen(5500);

我的index.html:

<!doctype html>
<html>
    <head>

        <title>Communicating from Node.js to an Arduino</title>
        <script src=".5.4/socket.io.min.js"></script>

    </head>
    <body>

        <h1>Communicating from Node.js to an Arduino</h1>

        <button id="lightOn">Turn Light On</button>
        <button id="lightOff">Turn Light Off</button>

        <script>
            
        var socket = io();

        document.getElementById('lightOn').onclick = function() {
            
            socket.emit('lights', { "status":"1" });
                
        };
            
        document.getElementById('lightOff').onclick = function(){
               
            socket.emit('lights', { "status":"0" });
            
        };
        
        </script>
        
    </body>
</html>
回答如下:
发布评论

评论列表(0)

  1. 暂无评论