Node.js学习之静态资源服务器


本文摘自PHP中文网,作者青灯夜游,侵删。

相关推荐:《nodejs 教程》

在创建 HTTP 服务器实现了一个最简单的静态资源服务器,可以对代码进行写改造,增加文件夹预览功能,暴露出一些配置,变成一个可定制的静态资源服务器模块

模块化

可定制的静态资源服务器理想的使用方式应该是这样的

1

2

3

4

5

6

7

8

9

10

const StaticServer = require('YOUR_STATIC_SERVER_FILE_PATH');

 

const staticServer = new StaticServer({

    port: 9527,

  root: '/public',

});

 

staticServer.start();

 

staticServer.close();

这样的使用方式就要求代码实现模块化,Node.js 实现一个模块非常简单

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

const http = require('http');

const fs = require('fs');

const path = require('path');

const mime = require('mime-types');

 

const defaultConf = require('./config');

 

class StaticServer {

  constructor(options = {}) {

    this.config = Object.assign(defaultConf, options);

  }

 

  start() {

    const { port, root } = this.config;

 

    this.server = http.createServer((req, res) => {

      const { url, method } = req;

 

      if (method !== 'GET') {

        res.writeHead(404, {

          'content-type': 'text/html',

        });

        res.end('请使用 GET 方法访问文件!');

        return false;

      }

 

      const filePath = path.join(root, url);

      fs.access(filePath, fs.constants.R_OK, err => {

        if (err) {

          res.writeHead(404, {

            'content-type': 'text/html',

          });

          res.end('文件不存在!');

 

        } else {

          res.writeHead(200, {

            'content-type': mime.contentType(path.extname(url)),

          });

          fs.createReadStream(filePath).pipe(res);

        }

      });

    }).listen(port, () => {

      console.log(`Static server started at port ${port}`);

    });

  }

 

  stop() {

    this.server.close(() => {

      console.log(`Static server closed.`);

    });

  }

}

 

module.exports = StaticServer;

完整代码:https://github.com/Samaritan89/static-server/tree/v1
执行 npm run test 可以测试
image.png
image.png

支持文件夹预览

当访问的路径是文件夹的时候程序会报错

1

2

3

4

5

6

7

8

Error: EISDIR: illegal operation on a directory, read

Emitted 'error' event on ReadStream instance at:

    at internal/fs/streams.js:217:14

    at FSReqCallback.wrapper [as oncomplete] (fs.js:524:5) {

  errno: -21,

  code: 'EISDIR',

  syscall: 'read'

}

因为 fs.createReadStream 尝试读取文件夹,需要兼容下访问路径是文件夹的时候,返回一个目录页,也就是在 fs.access 之后判断文件类型

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

fs.access(filePath, fs.constants.R_OK, err => {

  if (err) {

    res.writeHead(404, {

      'content-type': 'text/html',

    });

    res.end('文件不存在!');

 

  } else {

    const stats = fs.statSync(filePath);

    const list = [];

    if (stats.isDirectory()) {

      // 如果是文件夹则遍历文件夹,生成改文件夹内的文件树

      // 遍历文件内容,生成 html

 

    } else {

      res.writeHead(200, {

        'content-type': mime.contentType(path.extname(url)),

      });

      fs.createReadStream(filePath).pipe(res);

    }

  }

});

遍历生成 html 部分需要用到 文件夹操作 章节介绍的知识,为了方便生成 HTML,demo 使用了 Handlebar 模板引擎,主要逻辑

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

if (stats.isDirectory()) {

  // 如果是文件夹则遍历文件夹,生成改文件夹内的文件树

  const dir = fs.opendirSync(filePath);

  let dirent = dir.readSync();

  while (dirent) {

    list.push({

      name: dirent.name,

      path: path.join(url, dirent.name),

      type: dirent.isDirectory() ? 'folder' : 'file',

    });

    dirent = dir.readSync();

  }

  dir.close();

 

  res.writeHead(200, {

    'content-type': 'text/html',

  });

 

  // 对文件顺序重排,文件夹在文件前面,相同类型按字母排序,不区分大小写

  list.sort((x, y) => {

    if (x.type > y.type) {

      // 'folder' > 'file', 返回 -1,folder 在 file 之前

      return -1;

    } else if (x.type == y.type) {

      return compare(x.name.toLowerCase(), y.name.toLowerCase());

    } else {

      return 1;

    }

  });

 

  // 使用 handlebars 模板引擎,生成目录页面 html

  const html = template({ list });

  res.end(html);

}

通过 git 代码修改记录可以清晰看到本次的变更:https://github.com/Samaritan89/static-server/commit/5565788dc317f29372f6e67e6fd55ec92323d0ea

同样在项目根目录执行 npm run test ,使用浏览器访问 127.0.0.1:9527 可以看到目录文件的展示
image.png
完整代码:https://github.com/Samaritan89/static-server/tree/v2

更多编程相关知识,请访问:编程教学!!

以上就是Node.js学习之静态资源服务器的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

node.js真的单线程吗?进程间如何通信?

node.js 15正式版发布,将替代node.js 14成为当前的的稳定发行版

浅谈node.js中es6导入语法的使用方法

http协议发展过程一览

node.js是什么?有什么优势?有什么用途?

深入了解node.js的中worker threads(工作线程)

深入浅析with的使用

避免node.js模块日志污染程序日志的方法介绍

node.js进行调试的几种方法介绍

nodejs如何升级版本?两种升级方法分享

更多相关阅读请进入《node.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...