【发布时间】:2016-08-29 19:48:44
【问题描述】:
我是节点的初学者。我正在尝试提供文件。我的项目的层次结构看起来像
应用程序
modules
node_modules
public
css
index.css
html
index.html
javascript
routes
main.js
在 main.js 中
var express = require('express');
var app = express();
var path = require('path')
var port = 8080;
app.use("/styles", express.static(path.join(__dirname + '../public/css')));
app.use("/scripts", express.static(__dirname + '../public/javascript'));
app.get('/' , function( req , res ){
res.sendFile(path.join(__dirname,'../public/html/index.html'))
})
app.listen(port)
我想在 / 路由上提供文件。它工作正常,但没有加载 css 和 javascript - 它在浏览器控制台中引发错误
http://localhost:8080/css/index.css资源加载失败: 服务器响应状态为 404(未找到)
设置css路径的正确方法是什么?我很难找到正确的解决方案。
谢谢!
【问题讨论】:
-
你的静态css路由被称为“styles”,而不是“css”;试试
http://localhost:8080/styles/index.css -
@Hamms 也找不到文件,为什么是样式?