Ask coding questions

← Back to all posts
script.js to html link - node.js
somecodingguy (0)

I want that code in script.js to work but its not. Am I linking it wrong?

Comments
hotnewtop
themining72 (49)

The file is currently just there, you must first add it to the web server.

Add this at line 8 in index.js:

app.get('/script.js', (req, res) => {
  res.sendFile(__dirname + '/index/script.js');
});

Warning: This method is not very efficient for production, for a bigger project please don't try this, it would work but I don't recommend it.

somecodingguy (0)

@themining72 thank you! You said its not efficient, so is there any other way to do it?

themining72 (49)

@somecodingguy The other way is for big projects basically, where you have multiple static files. In this case, refer to this documentation. Also could you set the previous comment above as the answer?