Tuesday, 4 April 2017

run a node.js server with html-website that interacts with the website

I’m new in node.js and javascript and want to learn the basics. So what I want to do is to run a server which hosts a html-website. On that html-website should be an input text field and a ‘Send’ button. Everytime when I click on send, the text I wrote in the input text field should be send to the server and printed on the console. I tried a few things so far but nothing worked :/ Here is the code for the 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);

And here is the code for the html website: 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>

Can somebody please explain how I can ‘connect’ the text field with the server so that there is an interaction possible? Big thx



via Bananajoe

No comments:

Post a Comment