1

I am trying to serve the index.html file from the node server, but I am getting this error "localhost/:1 Refused to execute the script from 'http://localhost:3001/assets/app.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Common JS Example</title>
</head>
<body>
    <h1>CommonJS</h1>
    <script type="text/javascript" src="./assets/app.js"></script>
</body>
</html>

server.js

var express = require('express');
var path = require('path');
var app = express();


app.use(express.static(path.resolve(__dirname, "assets")));

app.get('/',function(req,res){
    res.sendFile(path.resolve(__dirname, "./index.html"))
})

app.listen(3001, () => console.log('Example app listening on port 3001!'))

app.js

console.log('hiiii')

1 Answers1

2

"assets" is the static path, so modify the script src to

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

and it should work

Explanation here

likeabbas
  • 115
  • 1
  • 10