Replies: 2 comments 1 reply
-
Mybe typo mistake - Content-Type header, not a Context |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When<script type="module" src="./client. js"><script>in index. html causes the following error:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
If <script type="module" src="./client. js"><script>in index. html ,then it can work
The code is as follows:
server.js:
app.ws('/*', {
compression: uWS.DISABLED,
maxPayloadLength: 16 * 1024 * 1024,
idleTimeout: 10,
open: (ws) => {},
message: (ws, message) => {}
}).get('/', (res, req) => {
const html = readFileSync(
${process.cwd()}/public/index.html
, 'utf-8')res.writeHeader('Content-Type', 'text/html')
res.end(html)
}).any('/*', (res, req) => {
const url = req.getUrl()
const fileExt = url.split('.').slice(-1)[0].toLocaleLowerCase()
try {
switch (fileExt) {
case 'js':
// res.writeHeader('Context-Type', 'text/javacript')
res.writeHeader('Context-Type', 'application/javacript')
break;
case 'css':
res.writeHeader('Context-Type', 'text/css')
break;
case 'jpg':
res.writeHeader('Context-Type', 'application/x-jpg')
break;
}
}
catch (err) {
res.end((err as any).toString())
}
}).listen(port, (token) => {
if (token) {
process.stdout.write('\x1b[33m')
process.stdout.write(
Listening to port ${port}\n
)} else {
process.stdout.write('\x1b[31m')
process.stdout.write(
Failed to port ${port}\n
)}
process.stdout.write('\x1b[0m')
})
public/index.html:
<title>feathers-chat</title> <script type="module" src="./client.js"></script>public/client.js:
const chatTemplate = () =>
<div> <h1>hello world</h1> <div> <ul> <i>length1</i> <i>length2</i> </ul> </div> </div>
const showLogin = () => {
document.getElementById('app').innerHTML = chatTemplate()
}
showLogin()
tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"noImplicitThis": false,
"paths": {
"@/": [ "./" ]
}
},
"include": [ "/*.ts", "/*.js" ]
}
Beta Was this translation helpful? Give feedback.
All reactions