我是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