我的nodejs应用程序中没有提供css文件
大家好,我不明白为什么当url包含参数时,我的css文件没有被提供。当我点击这里的链接时,我想从另一条路由访问一条路由:我的nodejs应用程序中没有提供css文件,css,node.js,express,view,Css,Node.js,Express,View,大家好,我不明白为什么当url包含参数时,我的css文件没有被提供。当我点击这里的链接时,我想从另一条路由访问一条路由:位于欢迎文件中。下面是我的服务器配置:app.js let express = require("express"); let path = require("path"); let mongoose = require("mongoose"); let bodyParser = require('body-parser'); let exphbs
位于欢迎
文件中。下面是我的服务器配置:app.js
let express = require("express");
let path = require("path");
let mongoose = require("mongoose");
let bodyParser = require('body-parser');
let exphbs = require('express-handlebars');
let dotenv = require("dotenv").config();
let app = express();
app.use(express.static(path.join(__dirname,"public")))
app.set("views","views");
app.set("view engine","hbs");
app.engine("hbs",exphbs({
extname:"hbs",
layoutsDir:path.join(__dirname,"views"),
defaultLayout:"template",
partialsDir:path.join(__dirname,"views/partials/")
})) ;
######ROUTING
app.get("/",(req,res)=>{
res.render("welcome") // from which I access "/books/:id" route : css file is served
})
app.get("/books/:id",(req,res)=>{
let p_id = req.params.id;
//here css file is not served but if I access without parameter,css file is served so WHY??????
res.render("bookdetail")
})
NB:mycss文件名为home.css
我的模板文件template.hbs
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="home.css">
<title>ziri book</title>
</head>
<body>
{{{body}}}
</body>
</html>
齐里书
{{{body}}}
您的css文件路径相对于页面url。页面URL具有不同的级别数/欢迎使用root,但/book/123比root低一步。解决方案:如果您将站点托管在服务器根目录下,或在其前面加上{{rootpath}之类的前缀,请使用href=“{rootpath}}/home.css”>
where{{rootpath}}指向站点根目录的路径。@SergeyL感谢您的回复,它可以使用href=/home.css