Express 在嵌套目录中提供静态文件

5 ejs static-files node.js express

我对使用快速和渲染模板视图并不陌生,但最近我尝试了一种新的目录结构,这似乎使我的应用程序实例感到困惑。

它不再为我的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)
Run Code Online (Sandbox Code Playgroud)

静态文件似乎比根目录深两层。在我的app.js文件中,我尝试express static method像这样使用:

app.use( express.static(path.join(__dirname, './src' + '/public')));
Run Code Online (Sandbox Code Playgroud)

并且还尝试过这个:

app.use('/src', express.static(path.join(__dirname, '/public')));
Run Code Online (Sandbox Code Playgroud)

在我看来,例如静态的调用方式如下:

<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="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.2/classic/ckeditor.js"></script>
Run Code Online (Sandbox Code Playgroud)

但仍然无法将这些文件提供给我的观点。我被困在这个问题上。有人可以帮我吗?谢谢

tho*_*061 6

尝试这个:

app.use(express.static(path.join(__dirname, 'src/public')));
Run Code Online (Sandbox Code Playgroud)

和这个:

<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">
Run Code Online (Sandbox Code Playgroud)