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

编程入门 行业动态 更新时间:2024-10-07 14:26:56

将值从 html 网页传递到 Nodejs <a href=https://www.elefans.com/category/jswz/34/1771423.html style=服务器"/>

将值从 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>
回答如下:

更多推荐

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

本文发布于:2024-05-30 14:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   网页   html   Nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!