我是node.js和javascript中的新成員,希望瞭解基礎知識。所以我想要做的是運行一個託管html網站的服務器。在該HTML網站上應該是一個輸入文本字段和一個「發送」按鈕。每次點擊發送時,我在輸入文本字段中寫入的文本都應發送到服務器並打印在控制檯上。到目前爲止,我已經嘗試了幾件事,但沒有成功。使用與網站交互的html-website運行node.js服務器
這裏是server.js代碼:
var http = require('http');
var fs = require('fs');
function handleRequest(request, response) {
if(request.method == 'GET' && request.url == '/'){
response.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream("./index.html").pipe(response);
}
}
var server = http.createServer(handleRequest);
server.listen(3000);
這裏是爲HTML網頁代碼: 的index.html
<html>
<head>
<title>title</title>
</head>
<body>
<h1>text field</h1>
<input type="text" id="input1" name="input1">
<input type="submit" id="submitinput1" value="Send" onclick="sendClick()">
</body>
</html>
有人可以請解釋一下我怎麼能「連接'與服務器的文本字段,以便有可能的交互?
從閱讀介紹性HTML表單教程開始。 – Quentin
你的問題並沒有關聯到nodejs,而是基礎知識。請查看本教程https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 – flotto