Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js Express无法获取静态文件,路由比一个文件夹级别更深_Javascript_Css_Node.js_Express - Fatal编程技术网

Javascript Node.js Express无法获取静态文件,路由比一个文件夹级别更深

Javascript Node.js Express无法获取静态文件,路由比一个文件夹级别更深,javascript,css,node.js,express,Javascript,Css,Node.js,Express,在我提问之前,这里是我的node.js express应用程序的文件夹结构: /home server.js index.html /应用程序 /路线 public.js /公开的 /css main.css 这是我在server.js中看到的: // ROUTING FOR STATIC CONTENT var public_dir = __dirname + '/public'; app.use(express.static(public_dir)); // INITIALIZE ROU

在我提问之前,这里是我的node.js express应用程序的文件夹结构:

/home
server.js
index.html
/应用程序
/路线
public.js
/公开的
/css
main.css

这是我在
server.js中看到的:

// ROUTING FOR STATIC CONTENT
var public_dir = __dirname + '/public';
app.use(express.static(public_dir));

// INITIALIZE ROUTER
var public_router = express.Router();

// IMPORT PUBLIC ROUTES
require('./app/routes/public')(public_router, public_dir);

// REGISTER ROUTES
app.use('/', public_router);
var path = require('path');

module.exports = function(public_router, public_dir) {

    public_router.get('/new', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });

    public_router.get('/popular', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });

    public_router.get('/popular/today', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });
}
这是我在
/app/routes/public.js中看到的:

// ROUTING FOR STATIC CONTENT
var public_dir = __dirname + '/public';
app.use(express.static(public_dir));

// INITIALIZE ROUTER
var public_router = express.Router();

// IMPORT PUBLIC ROUTES
require('./app/routes/public')(public_router, public_dir);

// REGISTER ROUTES
app.use('/', public_router);
var path = require('path');

module.exports = function(public_router, public_dir) {

    public_router.get('/new', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });

    public_router.get('/popular', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });

    public_router.get('/popular/today', function(req, res) {
        res.sendFile(path.join(public_dir, 'index.html'));
    });
}
index.html
中,我这样调用css文件:

<link rel="stylesheet" href="css/main.css">

问题:

当我访问mydomain.com/newmydomain.com/popular时,css文件被加载,页面被正确呈现。但是,当我访问mydomain.com/popular/today时,css文件没有正确加载。在chrome开发工具中,我看到浏览器试图从流行文件夹加载css,这是不正确的

任何想法或意见都将受到欢迎。 提前谢谢

更改为

注意前缀
/
。这将使浏览器开始在树的根目录中搜索,而不是使用相对路径。

更改为


注意前缀
/
。这将使浏览器开始在树的根目录中搜索,而不是使用相对路径。

我的朋友,你说得对!非常感谢!:)谢谢@Sawny,我没有别的办法。在过去的两天里,我一直在寻找解决方案。你我的朋友确实是对的!非常感谢!:)谢谢@Sawny,我没有别的办法。在过去的两天里,我们一直在寻找解决方案。