I am getting mime type mismatch messages while trying to serve an HTML file named index.html with NodeJS. The warnings are as followed:
The stylesheet http://localhost:8081/styling.css was not loaded because its MIME type, "text/html", is not "text/css".
The script from “http://localhost:8081/script.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
Error Screenshot:
Listing of Files Screenshot:
NodeJS Server Code:
#!/usr/bin/env node
const FS = require('fs')
const HTTP = require('http')
const Server = HTTP.createServer( response )
const Path = require('path')
let PORT = 8080
listen = () => {
Server.listen(PORT, 'localhost').on('error', e => {
console.log(`Trying to listen to port ${++PORT}`)
listen()
})
}
function response(r, c) {
let d = new Date()
console.log(`Respond Fired at ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`)
c.setHeader('Content-Type', 'text/plain')
c.writeHead(200, { 'Content-Type': 'text/html' } )
c.write(FS.readFileSync(Path.join(__dirname, 'index.html')))
c.end()
}
listen()
Contents of index.html:
<!Doctype HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styling.css">
</head>
<body>
<p>Hello World!</p>
<script type="application/javascript" src="script.js"></script>
</body>
</html>
Contents of styling.css:
p {
color: #f55 ;
}
Contents of script.js:
document.write('Hello World from JavaScript!')
Browser:
Firefox 73.0b9 (64-bit)