我想通过Node.js使用express()函数在localhost:3000中将CSS添加到我的HTML中。 不幸的是,有些奇怪。我一步一步按照教程中的步骤,但仍然无法加载我的CSS。我的style.css在css文件夹(css/style.css)。这里是我的代码:
app.js(请注意,我用的应用程序和应用1)CSS无法使用Node.js加载到我的HTML代码中
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var express = require('express');
var app1 = express();
var mySocket = 0;
app1.use(express.static('/css'));
app.listen(3000); //Which port are we going to listen to?
function handler (req, res) {
fs.readFile(__dirname + '/index.html', //Load and display outputs to the index.html file
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
console.log('Webpage connected'); //Confirmation that the socket has connection to the webpage
mySocket = socket;
});
//UDP server on 41181
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("Broadcasting Message: " + msg); //Display the message coming from the terminal to the command line for debugging
if (mySocket != 0) {
mySocket.emit('field', "" + msg);
mySocket.broadcast.emit('field', "" + msg); //Display the message from the terminal to the webpage
}
});
server.on("listening", function() {
var address = server.address(); //IPAddress of the server
console.log("UDP server listening to " + address.address + ":" + address.port);
});
server.bind(41181);
的style.css(CSS/style.css文件)
.test
{
color:red;
}
指数.html
var socket = io.connect('http://localhost:3000');
socket.on('field', function (data) {
console.log(data);
$("#field").html(data);
});
Data from C#:
-09-13
Mehdi
+0
你有没有在控制台中的任何错误的最小工作的例子吗?例如,像style.css的404一样。 –
+0
'css'文件的URL以'/'开头。确保'localhost'的根目录包含'css'文件夹。 –