表达静态资产不能正常工作的东西/东西

Mic*_*bry 0 node.js express

在我的快递应用中,静态资产将无法投放something/something.

/**
 * Express configuration.
 */
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// app.use(compress());
// app.use(assets({
//   paths: ['public/css', 'public/js']
// }));
app.use(logger('dev'));
app.use(favicon(path.join(__dirname, 'public/favicon.png')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(methodOverride());
app.use(cookieParser());
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: secrets.sessionSecret,
  store: new MongoStore({ url: secrets.db, autoReconnect: true })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(lusca({
  csrf: true,
  xframe: 'SAMEORIGIN',
  xssProtection: true
}));
app.use(function(req, res, next) {
  res.locals.user = req.user;
  next();
});
app.use(function(req, res, next) {
  if (/api/i.test(req.path)) req.session.returnTo = req.path;
  next();
});
app.use(express.static(__dirname + '/assets'));
Run Code Online (Sandbox Code Playgroud)

然后在我的主要布局

<link rel="stylesheet" href="css/main.css">
Run Code Online (Sandbox Code Playgroud)

主页有效,但不是嵌套路由?

jfr*_*d00 5

样式表href是一个相对链接,这意味着浏览器会将其解释为相对于当前网页的路径,因此对于嵌套路径,它将以不同于顶级网页的方式进行插入.您可能需要将链接更改为:

<link rel="stylesheet" href="/css/main.css">
Run Code Online (Sandbox Code Playgroud)

所以它被解释为相同,无论你把它放在什么网页的路径恰好.

给定您的express.static()代码,然后"/css/main.css"将查找此路径__dirname + "/assets/css/main.css".