Why isn't NodeJS loading CSS style sheets linked to html files?

前端 未结 2 1562
南方客
南方客 2021-01-28 17:09

community. I am at a bit of a loss here. I am relatively new to NodeJS, and the runtime environment is acting in a way I didn\'t expect. It turns out this has been a problem for

2条回答
  •  死守一世寂寞
    2021-01-28 17:26

    Because your server-side code is written to handle all.http requests and deliver the same html content, regardless of the path.

    try adding some if-else logic inside your handler, and deliver appropriate file based on the request path.

    something like:

    if(req.path === "" || req.path === "index.html")
         fs.read htnl file here
    else if (req.path==="my.css")
         fs.read css file
    

    learn to use browser dev tools (F12), which shows you exactly which requests the browser is making, what it sends, what it gets back - amongst many other things.

提交回复
热议问题