user3547774
user3547774

Reputation: 1699

express service static html referencing js files

I have the following folder structure in my node project (using node, webpack, and express):

The index.html (located in the above mentioned views folder) file contains the following:

<script type="text/javascript" src="../dist/test.js" ></script>

In the app.js for the node application I am serving the static .html page in the following manner:

router.get('/setup', function(req, res){
    res.sendFile(path.join(__dirname, './views/index.html'));
});

The problem is that the .html renders properly but it is unable to find the .js file which it references. How do I fix this?

Upvotes: 0

Views: 79

Answers (3)

pb_
pb_

Reputation: 492

You can do the following -

app.use(express.static('dist'));
router.get('/setup', function(req,res) {
  res.sendFile(__dirname + '/views/hello.html');
})

And in your html file just do this -

<script type="text/javascript" src="test.js" ></script>

Upvotes: 0

midhun k
midhun k

Reputation: 1068

Use this src="./dist/test.js" instead of src="../dist/test.js". May be this will be the problem

Upvotes: 0

Godfather
Godfather

Reputation: 5901

Inside your app.js file add line:

app.use('/source/', express.static(__dirname + '/path_to_js_folder'));

And modify your < script > tag as:

<script type="text/javascript" src="/source/test.js" ></script>

Upvotes: 0

Related Questions