0

Hello I havent used vanilla js in a couple months and went to go make a simple count function like the basic react one, I know the logic works because it works when I run it with live preview from vs code, but when I run the server and visit the local host port it does not work, what am i doing wrong?

i have server js which looks like

 const express = require("express");

const app = express();

app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
})

app.listen(3000, function(){
    console.log("server started on port 3000");
});

the file where the logic is

var count = 0;

const addBtn = document.getElementById("add");

addBtn.addEventListener("click", () => handleClick());

function handleClick() {
  count++;
  console.log(count);
  document.getElementById("number").innerHTML = count;
}

and the html

<body>
    <h1>
        "hello".
        
    </h1>
    <h1 id="number">0</h1>
    <button id="add">Click me</button>

    <script src='app.js'></script>
</body>
Kevin.
  • 78
  • 1
  • 9
  • Your JavaScript file isn't set public. Add this line after `const app = express();`: `app.use(express.static("./directory/of/your/js/file/"));`. See https://stackoverflow.com/questions/10434001/static-files-with-express-js. – code Feb 17 '22 at 02:14
  • Glad it helped :) Happy coding! – code Feb 17 '22 at 07:06

0 Answers0