add_server_react

This commit is contained in:
Иван Медведев 2023-05-24 19:03:17 +03:00
parent adbf8b644d
commit 01b5f88e31
2 changed files with 38 additions and 2 deletions

20
index.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome file</title>
<link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>
<body class="stackedit">
<div class="stackedit__html"><h1 id="состояние-ардуины-отображается-в-браузере"><em>Состояние ардуины отображается в браузере:</em></h1>
<h2 id="ответ-на-hello">ответ на “hello”</h2>
<p>Пришел ответ:</p>
<h2 id="состояние-ардуины-равно">Состояние ардуины равно</h2>
<p>Пришел ответ:</p>
</div>
</body>
</html>

View File

@ -11,8 +11,10 @@
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const http = require('node:http');
const fs = require('fs');
const port = new SerialPort({ path: 'COM9', baudRate: 9600 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
var value_arduino = "";
@ -25,16 +27,30 @@ parser.on('data', (data => {
}));
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
console.log( req.url);
if (req.url == "/hello") {
res.end("/hello");
}else
{
if (req.url == "/") {
fs.readFile('D:/medvedevis/index.html', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end(data);
});
}
else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(value_arduino);
console.log( req.url);
}
}}
});