[英]CSS and JS files don't load on node.js server
This is my node.js file:这是我的 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');
});
I looked up multiple questions but they didn't seem to help.我查了多个问题,但它们似乎没有帮助。 I'm new to node.js and web programming as a whole.
我是 node.js 和 web 整体编程的新手。 And also my folder organization:
还有我的文件夹组织:
Use app.use(express.static(path.join(__dirname, 'public')));
使用
app.use(express.static(path.join(__dirname, 'public')));
instead.反而。 So that everything inside
/public
is accessible by your html page这样您的 html 页面就可以访问
/public
中的所有内容
Use this in your server.js file在你的 server.js 文件中使用它
app.use(express.static(__dirname + '/public'));
and add css like并添加 css 之类的
<link rel="stylesheet" type="text/css" href="style.css" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.