Express在嵌套目录中提供静态文件
我不熟悉使用快速和渲染模板视图,但最近我尝试了一个新的目录结构,似乎混淆了我的app实例。
它不再服务我的static bootstrap, css and image
文件了,我的视图现在看起来都搞砸了。
这是我的目录结构
AppName
- node_modules folder
- src (all code live here)
- public (statics)
- css/mycss, bootstrapcss
- js/ myjs, bootstrapjs
- images folder
- models
- app.js (entry point)
静态文件似乎比根目录深两级。在我的app.js
文件中,我尝试过像这样使用express static method
:
app.use( express.static(path.join(__dirname, './src' + '/public')));
并试过这个:
app.use('/src', express.static(path.join(__dirname, '/public')));
在我看来,例如静态就像这样调用:
<link href="../public/css/bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../public/fonts/glyphicons-halflings-regular.ttf">
<link rel="stylesheet" href="../public/css/styles.css" type="text/css">
<script defer src=".0.8/js/all.js"></script>
<script src=".0.0-alpha.2/classic/ckeditor.js"></script>
但仍然无法将这些文件提供给我的观点。我坚持这个。有人可以帮我吗?谢谢
回答如下:试试这个:
app.use(express.static(path.join(__dirname, 'src/public')));
还有这个:
<link href="/css/bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/fonts/glyphicons-halflings-regular.ttf">
<link rel="stylesheet" href="/css/styles.css" type="text/css">
Express在嵌套目录中提供静态文件
我不熟悉使用快速和渲染模板视图,但最近我尝试了一个新的目录结构,似乎混淆了我的app实例。
它不再服务我的static bootstrap, css and image
文件了,我的视图现在看起来都搞砸了。
这是我的目录结构
AppName
- node_modules folder
- src (all code live here)
- public (statics)
- css/mycss, bootstrapcss
- js/ myjs, bootstrapjs
- images folder
- models
- app.js (entry point)
静态文件似乎比根目录深两级。在我的app.js
文件中,我尝试过像这样使用express static method
:
app.use( express.static(path.join(__dirname, './src' + '/public')));
并试过这个:
app.use('/src', express.static(path.join(__dirname, '/public')));
在我看来,例如静态就像这样调用:
<link href="../public/css/bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../public/fonts/glyphicons-halflings-regular.ttf">
<link rel="stylesheet" href="../public/css/styles.css" type="text/css">
<script defer src=".0.8/js/all.js"></script>
<script src=".0.0-alpha.2/classic/ckeditor.js"></script>
但仍然无法将这些文件提供给我的观点。我坚持这个。有人可以帮我吗?谢谢
回答如下:试试这个:
app.use(express.static(path.join(__dirname, 'src/public')));
还有这个:
<link href="/css/bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/fonts/glyphicons-halflings-regular.ttf">
<link rel="stylesheet" href="/css/styles.css" type="text/css">