From c1d2600f9205a32d22a971f903a504cfaa45dcc8 Mon Sep 17 00:00:00 2001 From: Serditova Sofya Date: Thu, 11 May 2023 18:58:04 +0300 Subject: [PATCH] =?UTF-8?q?https://beautifier.io/=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D1=82=D1=8C=20=D0=BA=D0=BE=D0=B4=20=D0=BA=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D0=B8=D0=B2=D1=8B=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 2b95aaa..091fb0f 100644 --- a/index.js +++ b/index.js @@ -8,50 +8,61 @@ // port.on('readable', function () { // console.log('Data:', port.read()) // }) - + // // Switches the port into "flowing mode" // port.on('data', function (data) { // console.log('Data:', data.toString('UTF8')) // }) const fs = require('fs'); const http = require('node:http'); -const { SerialPort } = require('serialport') -const { ReadlineParser } = require('@serialport/parser-readline') +const { + SerialPort +} = require('serialport') +const { + ReadlineParser +} = require('@serialport/parser-readline') -const port = new SerialPort({ path: '/com9', baudRate: 9600 }) +const port = new SerialPort({ + path: '/com9', + baudRate: 9600 +}) var str = ""; -const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' })) +const parser = port.pipe(new ReadlineParser({ + delimiter: '\r\n' +})) parser.on('data', (data) => { - if (str != data) { - console.log(data); - } - str = data; + if (str != data) { + console.log(data); + } + str = data; }) // Create a local server to receive data from const server = http.createServer((req, res) => { console.log(req.url); -if(req.url=='/'){ - fs.readFile('index.html', (err,data) => { - if(err){ + if (req.url == '/') { + fs.readFile('index.html', (err, data) => { + if (err) { res.writeHead(404, "Not Found"); res.write("No file found") res.end(); } else { res.writeHead(200, { - 'Content-Type': 'text/html' + 'Content-Type': 'text/html' }) res.write(data) res.end(); } }) - } else if(req.url=='/pin'){ - res.writeHead(200, {'Content-Type': 'application/json'}); + } else if (req.url == '/pin') { + res.writeHead(200, { + 'Content-Type': 'application/json' + }); res.end(str); - } else{ + } else { res.writeHead(404, "Not Found"); res.end(); }