0
为什么我的css文件只能看似随机加载?我该如何解决这个问题?出于某些原因,我没有使用Express来使用Nodejs。随机地,我的css不会加载我的应用程序
var http = require('http');
var fs = require('fs');
http.createServer(function (request, response) {
console.log('request starting...');
fs.readFile('./view.html', function(error, content) {
if(error) {
response.writeHead(404);
response.end();
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(content, 'utf-8');
}
});
fs.readFile('./css/appStylesheet.css', function(error, content) {
if(error) {
response.writeHead(404);
response.end();
} else {
response.writeHead(200, {'Content-Type': 'text/css'});
response.end(content, 'utf-8');
}
});
}).listen(3000);
console.log('Server running at localhost on port 3000');
以防万一,这部分不是问题的所在,下面的代码显示HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>Sign In</title>
<link rel="stylesheet" type = "text/css" href="/css/appStylesheet.css">
</head>
<body>
<p id = "signIn">
<p>Username</p>
<input type = "text" id = "username" value = "" >
<script>
function username() {
var x = document.createElementById("username").value;
document.getElementById("demo").innerHTML = x;
}
</script>
<p>Password</p>
<input type = "text" id = "password" value = "">
<p><button onclick = "password()">Submit</button></p>
<script>
function password() {
var x = document.createElementById("password").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</p>
</body>
</html>