我具有以下文件结构:
(http://localhost:3000/Chart.js:10917:4)
dashboard:178 Uncaught TypeError: Cannot read property 'style' of null
at ChartElement.customTooltips (dashboard:178)
at ChartElement.update (Chart.js:8660)
at ChartElement.handleEvent (Chart.js:8952)
at Chart.eventHandler (Chart.js:4679)
at listener (Chart.js:4609)
at HTMLCanvasElement.proxies.(anonymous)
然后在我的- server.js
- controllers
- [...]
- public
- utils
- views
- home
- index.html
- js
- index.js
- css
- index.css
内部,在应用程序启动时执行此操作:
server.js
这是我“回家”路线中的逻辑。
app.use(express.static( path.join(__dirname, "public") ));
当我导航到app.get("/", (req, res) => {
const publicViews = path.join(__dirname, "public", "views");
res.sendFile("home/index.html", { root: publicViews });
});
时index.html
呈现时,服务器未找到并返回localhost:{port}/
和index.js
。
这就是我在index.css
中引用它们的方式
index.html
<link rel="stylesheet" href="./css/index.css">
我在做什么错了?
答案 0 :(得分:1)
You can add one more static folder
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views', 'home')));
app.use('/', index);
app.use('/users', users);
Then it will be possible to use your templates like this
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('home/index', { title: 'Express' });
});
module.exports = router;
And your assets will be accessible
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
</title>
</head>
<body>
<script src="/js/index.js"></script>
</body>
答案 1 :(得分:1)
Feel free to assign method specific routing instead of using static middleware for serving resources from /public/utils/
.
app.use('/utils', express.static(path.join(__dirname, 'public', 'utils')))
app.use('/', express.static(path.join(__dirname, 'public', 'views', 'home')))