代码之家  ›  专栏  ›  技术社区  ›  Vitor Paulino

node express中的静态文件路由不工作

  •  0
  • Vitor Paulino  · 技术社区  · 8 年前

    我在那里,

    我已经找到了一个似乎是我的问题的答案,并且我已经实现了可以在与我的问题相关的答案中找到的实现,但是它们不起作用。

    开发机器:Windows10Pro

     "express": "^4.16.3",
      "request": "^2.87.0",
      "serve-static": "^1.13.2",
    

    目录文件夹

    |app
    | |- web
    | |  |-pages
    | |  |   - <app domain folder>
    | |  |    |  - index.html
    | |  |    |- routeHandler.js    
    | |  |-static
    | |  | - js
    | |  |   | - appClient.ls
    |infrascruture
    | - web
    | | -builder.js 
    |index.js 
    

    在buider.js上,我拥有启动服务器的所有快速样板代码,包括静态中间件: 这样地:

    var serveStatic = require("serve-static");
    
    module.exports = (function(express)
    {
    module = {};
    module.webApp =  express();
    
    module.addAppComponents = function() {}
    module.addStaticMiddleware = function()
    {
     module.webApp.use('static', serveStatic(path.join(__dirname, "/app/web/static/js/")));
    }
    return module;
    });
    

    app domain文件夹上的index.html被提供给正在工作的路由,该html为:

    <!DOCTYPE html>
    <html>
        <script src="/static/clientApp.js" ></script> 
        <body onload="loaded()">
            <h1> Endpoints</h1>
            <h2> These are the endpoints available to subscribe</h2>
        </body>
    
    </html> 
    

    浏览器正在执行的请求包括:

    // fetching the index.html
    request: GET url: localhost:8080/pages/endpoints/{}
    headers: {"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9"}
    body: "{}"
    
    // fetching the javascript but not working
    request: GET url: localhost:8080/static/clientApp.js{}
    headers: {"host":"localhost:8080","connection":"keep-alive","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","accept":"*/*","referer":"http://localhost:8080/pages/endpoints/","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9"}
    

    有什么想法吗?

    提前通知!

    1 回复  |  直到 8 年前
        1
  •  0
  •   Lalit Goswami    8 年前

    使用

    module.webApp.use('static', serveStatic('../../app/web/static/js/')));
    

    而不是

    module.webApp.use('static', serveStatic(path.join(__dirname, "/app/web/static/js/")));

    推荐文章