简体   繁体   English

服务静态文件节点

[英]Serving Static Files Node

I am trying use only Node (no additional npms or express). 我正在尝试仅使用Node(没有其他npms或express)。 Here is my current route handler: 这是我当前的路线处理程序:

function requestHandler(req, res) {

    var localFolder = __dirname + '/views/',
            page404 = localFolder + '404.html',
            fileToServe = "";

    if(/\/posts\/\d+/.test(req.url)){   
        fileToServe = __dirname + req.url.match(/\/posts\/\d+/) +'.json';
        fs.stat(fileToServe, function(err, contents){
            if(!err && contents){
                res.end(templateEngine('post',fileToServe));
            } else {
                res.end(templateEngine('error', err))
            }
        });
    } else if (/\/posts\//.test(req.url)){

    } else if (/.+[^\W]$/.test(req.url)){
        fileToServe = __dirname + '/views' + req.url.match(/\/.+[^\W]$/gi);
        fs.stat(fileToServe, function(err, contents){
            if(!err && contents){
                res.end(fs.readFileSync(fileToServe));
            } else {
                res.end(templateEngine('error', err))
            }
        });
    }
}

I have two questions: 我有两个问题:

In one of my views if have a <link> tag with a css file. 在我的一种观点中,如果有一个带有CSS文件的<link>标记。 When I go straight to the path, it is served (this is the regex that catches it: /.+[^\\W]$/.test(req.url)) . 当我直接进入路径时,就可以使用它(这是捕获它的正则表达式: /.+[^\\W]$/.test(req.url)) . /.+[^\\W]$/.test(req.url)) However, as mentioned, one of my views built from the template engine uses the css file. 但是,如上所述,我从模板引擎构建的视图之一使用了css文件。

How does the browser work when it sees the link tag? 浏览器看到链接标记时如何工作? Does it send a GET request to my local server (node)? 它是否将GET请求发送到我的本地服务器(节点)? If it does, why doesn't my server send a response back? 如果可以,为什么我的服务器没有发回响应? When I go directly to the link, it sends the response perfectly fine. 当我直接转到链接时,它可以很好地发送响应。

Furthermore, when I try going to the page that uses the css file in the link tag, it just hangs on an empty page. 此外,当我尝试转到使用链接标记中的css文件的页面时,它只是挂在空白页面上。 When I kill the server, it says it never received a response (once again, when I go to the link directly, I get the proper file). 当我杀死服务器时,它说它从未收到响应(再次,当我直接转到链接时,我得到了正确的文件)。

Do I have to re-organize my routes? 我需要重新组织路线吗? Serve static files separately? 分别提供静态文件?

How does the browser work when it sees the link tag? 浏览器看到链接标记时如何工作? Does it send a GET request to my local server (node)? 它是否将GET请求发送到我的本地服务器(节点)?

Yes. 是。 Browser creates the full URL based on the current URL and makes an HTTP GET request like it does for any other resource. 浏览器根据当前URL创建完整URL,并像对其他任何资源一样发出HTTP GET请求。

If it does, why doesn't my server send a response back? 如果可以,为什么我的服务器没有发回响应? When I go directly to the link, it sends the response perfectly fine. 当我直接转到链接时,它可以很好地发送响应。

All evidence suggests that your page which links to the css is not being captured in the handler if-blocks. 所有证据表明,链接到css的页面未在处理程序if块中捕获。 Try putting a couple of console.log s, one right inside the requestHandler and the other inside in the block which is supposed to handle the page request. 尝试将两个console.log放置在requestHandler内,另一个放置在应该处理页面请求的块内。 I think you will only see one log show up in the server console. 我认为您只会在服务器控制台中看到一个日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM