[英]CSS and JS files don't load on node.js server
這是我的 node.js 文件:
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
var express = require('express');
var path = require('path');
app.use(express.static(path.join(__dirname, 'public/style.css')));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
我查了多個問題,但它們似乎沒有幫助。 我是 node.js 和 web 整體編程的新手。 還有我的文件夾組織:
使用app.use(express.static(path.join(__dirname, 'public')));
反而。 這樣您的 html 頁面就可以訪問/public
中的所有內容
在你的 server.js 文件中使用它
app.use(express.static(__dirname + '/public'));
並添加 css 之類的
<link rel="stylesheet" type="text/css" href="style.css" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.