joohny five를 사용하기 위해 html의 버튼에서 사용하기 위해 nodejs의 기능을 사용하는 방법은 무엇입니까?

Thecxoi

Express와 함께 nodejs를 사용하여 서버를 만듭니다.

server.js

const express = require('express');
const app = express();
const path = require('path');
const router = express.Router();
router.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/index.html'));
  //__dirname : It will resolve to your project folder.
});
//----------------------------------------------------------------------
//add the router
app.use(express.static(__dirname + '/View'));
//Store all HTML files in view folder.
app.use(express.static(__dirname + '/Script'));
//Store all JS and CSS in Scripts folder.
app.use('/', router);
app.listen(process.env.port || 3000);

그리고 javascript와 함께 html을 사용하십시오.

index.html

<html>
    <head>
        <meta charset="utf-8"/>
    </head>
    <link rel="stylesheet" href="./index.css">
    <script type="text" src="./server.js"></script>
    <script src="./index.js"></script>
    <body>
        <h1>Automatico</h1>
        <button onclick="autohabilitar();">Habilitar</button>
        <button onclick="autodeshabilitar();">deshabilitar</button>
        <br>
        <h1>Foco-1</h1>
        <button onclick="f1habilitar();" id="f1h">Habilitar</button>
        <button onclick="f1deshabilitar();"id="f1d">deshabilitar</button>
</body>
</html>

index.js

   document.getElementById("f1h").disabled=true;
   document.getElementById("f1d").disabled=true;
}

function autodeshabilitar(){
    document.getElementById("f1h").disabled=false;
    document.getElementById("f1d").disabled=false;
}

function f1habilitar(){
    document.getElementById("f1h").disabled=true;
    document.getElementById("f1d").disabled=false;
}

function f1deshabilitar(){
    document.getElementById("f1d").disabled=true;
    document.getElementById("f1h").disabled=false;
}

기능이 필요해

function apagarf1(){
  led1.off();
}

에있는 server.js 온 클릭 버튼의에서 사용하기에 ... 나는 HTML에 스크립트 함수를 내보내기 가져 오기 시도를 사용 조니 개의 다른 스크립트에서 ...

코린 티

저는 Johnny 5에 익숙하지 않습니다.하지만 브라우저에서 node.js 특정 항목에 액세스 할 수 없다는 것을 알고 있습니다.

가장 좋은 방법은 프런트 엔드 코드에서 호출하는 익스프레스에서 기본 API 엔드 포인트를 설정하는 것입니다. 해당 엔드 포인트에 도달하면 nodejs 함수를 트리거하여 LED를 끌 수 있습니다.

서버 파일에 다음을 추가하십시오.

app.get('/led-off', (req, res) => {

  apagarf1()

  return res.send('LED off');

});

프런트 엔드에서 해당 엔드 포인트에 fetch () 호출을 수행하면 작동합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관