第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將函數(shù)從客戶端傳遞到服務(wù)器(nodejs)

如何將函數(shù)從客戶端傳遞到服務(wù)器(nodejs)

慕的地6264312 2021-12-23 14:36:05
我有這個(gè)代碼:let http = require('http');let fs = require('fs');let handleRequest = (request, response) => {    response.writeHead(200, {        'Content-Type': 'text/html'    });    fs.readFile('./index.html', null, function (error, data) {        if (error) {            response.writeHead(404);            respone.write('Whoops! File not found!');        } else {            response.write(data);        }        response.end();    });};http.createServer(handleRequest).listen(8000);基本上,此代碼在 Node 上創(chuàng)建本地服務(wù)器并打開文件:'index.html'?,F(xiàn)在,我<button>在我的 'index.html' 上創(chuàng)建一個(gè),當(dāng)被點(diǎn)擊 (onclick) 調(diào)用一個(gè)名為 'hello' 的函數(shù)時(shí):function hello() {   console.log('hello world);}因此,當(dāng)單擊按鈕時(shí),瀏覽器控制臺(tái)中會(huì)顯示“hello world”,但我希望 nodejs 控制臺(tái)中顯示“hello world”,而不是瀏覽器。我怎樣才能實(shí)現(xiàn)它?
查看完整描述

1 回答

?
千萬里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以使用 nodeJS 和expressJS來實(shí)現(xiàn)它。要安裝快速運(yùn)行npm i express.


試試這個(gè)


let http = require('http');

let fs = require('fs');

let express = require('express');

let app = express();

app.get('/', function (req, res) {

    res.sendfile('./index.html');

})


app.get('/getData', function (req, res) {

    console.log("getData called.");

    res.send("res from getData function");

})


var server = app.listen(8000, function () {

    var host = server.address().address

    var port = server.address().port

    console.log("Example app listening at http://%s:%s", host, port)

})

<html>

  <head>

<script>

  function httpGet(theUrl) {

    var xmlhttp = new XMLHttpRequest();

    var url = "http://localhost:8000/getData";

    xmlhttp.onreadystatechange = function (res) {

      if (this.readyState == 4 && this.status == 200) {

        document.write(this.responseText);

      }

    };

    xmlhttp.open("GET", url, true);

    xmlhttp.send();

  }

</script>

</head>

<body>

  <button onclick="httpGet()">httpGet</button>

</body>


</html>


查看完整回答
反對(duì) 回復(fù) 2021-12-23
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)