Nodejs:有效地提供静态文件

时间:2018-01-05 16:15:01

标签: node.js

我目前正在使用vanilla nodejs服务器,我想知道是否有更好的方式来提供文件(客户端文件与服务器位于不同的目录中)。现在,在我的路由器文件中,我使用fs.readFilesSync加载所有需要的文件,然后我就像这样提供它们:

switch (path) {
    case '/':
      renderHTML(index, res);
      break;
    case '/home':
      renderHTML(index, res);
      break;
    case '/static/js/index/index.js':
      serveJS(indexJS, res);
      break;
    case '/static/css/style.css':
      serveCSS(styleCSS, res);
      break;
    case '/static/js/socket.io-client.js':
      serveJS(socketJS, res);
      break;
    case '/static/js/IOClient.js':
      serveJS(IOClientJS, res);
      break; 

我的目录与此类似:

\project
    \client
        \css
        \js
        index.html
    \server
        app.js
        router.js 

这是提供文件的正确方法,还是有更有效的方法?谢谢。

0 个答案:

没有答案
相关问题