I am using node.js on Windows, with express module to generate a static page that has a Submit form which I want to run an update()
function on the Server (i.e. not expose my js file publicly) when someone clicks "Submit" button.
PROBLEM:
It seems my update()
function fails to be recognized from the HTML. The following error is generated:
Uncaught ReferenceError: update is not defined at HTMLInputElement.onclick ((index):23)
I suspect this is the result of my node project not being set up correctly.
Server file (server.js):
var fs = require('fs');
var express = require("express");
var app = express();
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
//__dirname : It will resolve to your project folder.
});
//loading all files in public folder
//see instructions in https://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
app.listen(8080);
console.log("Running at Port 8080");
HTML file (index.html):
<!DOCTYPE html>
<html>
<body>
<div id="div1">
<p id="p1">This is a static paragraph.</p>
</div>
<p>
<script type="text/javascript" src="start.js"></script>
</p>
Input:
<br>
<input type="text" name="human_input" value="">
<br>
<input type="button" value="Submit" onclick="update()">
</body>
</html>
JS file (compute.js)
function update() {
console.log("this is running!")
}
^^ note, this is a sample script I am trying to get working.
My folder structure
/
- node_modules
-- *
- public
-- start.js
- index.html
- compute.js
- package-lock.json
- server.js
- start.js
Notes:
- calling
update()
fromonclick
worked previously when I was not using node.