I'm trying to add a JavaScript function that I wrote myself, to an HTML button which I also did myself. I'm having problems calling the document.getElementById() function because I'm working with Node.js. I read in some other forum, you can work around that with JSDOM. Now I'm having problems with that. Is there any other solution which is easier than the JSDOM-way? I also can't really use JSDOM because it would require a new installation which couldn't be done in the intire team time-wise.
JS code
const fetch = require("./fetch");
const jsdom = require('jsdom');
const http = require('http');
const fs = require('fs');
var url = require('url');
const port = 3000
const server = http.createServer(function(req, res) {
var q = url.parse(req.url);
var filename = "." + q.pathname;
console.log(req.url)
if (req.url == '/') {
res.writeHead(301, { Location: 'http://localhost:3000/frontpage.html' })
res.end()
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
fs.readFile(filename, function(error, data) {
if(error) {
res.writeHead(404)
res.write('Error: File Not Found')
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.write(data)
}
return res.end()
})
}
})
server.listen(port, function(error) {
if (error) {
console.log('Something went wrong', error)
} else {
console.log('Server is listening on port ' + port)
}
})
const { JSDOM } = jsdom;
const { document } = (new JSDOM('frontpage.html')).window;
global.document = document;
var button = document.getElementById("meinButton")
button.addEventListener("click", fetch.register_medication);
HTML Elements
<script scr="fetch.js"></script>
<button id="meinButton">Klick mich!</button>