为Express路由器内的子路由提供静态文件

时间:2017-03-07 08:54:16

标签: javascript node.js express routing

我有一切正常路线工作,例如localhost:3000 /费用等。

Screenshot of directory structure

但是当子路径出现时,它往往会产生一个问题,即我的静态文件不能提供给localhost:3000 / fees / cfees这样的页面。
如何向此子路径提供静态文件。

我用过

app.use(express.static(path.join(__dirname, 'public')));

加载静态文件

3 个答案:

答案 0 :(得分:0)

例如,如果您要使用 bootstrap.min.css ,它位于 c:\ my_project \ public \ css \ bootstrap.min.css 中您的模板文件必须是:

<!DOCTYPE html>
<html>

      <head>
             ...
             <link rel="stylesheet" href="/css/bootstrap.min.css">
      </head>

      <body>
            ...
      </body>

</html>

答案 1 :(得分:0)

还有更多要在你的路由器中这样做:

func saveFilters(bathAny: Bool, bath1: Bool, bath2: Bool, bath3: Bool, bath4: Bool){

    let context = getContext()

    let transc = FilterCoreDataObj(context: context)
    //SET ENTITY VALUES

    transc.bathAny = bathAny
    transc.bath1 = bath1
    transc.bath2 = bath2
    transc.bath3 = bath3
    transc.bath4 = bath4

    do {
        try context.save()
        print("saved")
    } catch let error as NSError {
        print("could not save \(error), \(error.userInfo)")
    } catch {

    }
}

答案 2 :(得分:0)

您应该使用绝对路径而不是相对路径

例如,使用&#34; /bundle.js &#34;而不是&#34; bundle.js&#34;