JavaScript
主页 > 网络编程 > JavaScript >

Node如何实现在浏览器预览项目的所有图片介绍

2023-01-09 | 佚名 | 点击:

背景

在前端实际项目开发中,会有这样一种场景。每次引入新的图片,并不知道这个资源是否被引用过,所以会点开存放图片的资源一个个去看。实际问题是:

如果有个能力,将项目图片资源罗列到一起查看,并方便看到引入路径的话,就会大大节约开发的体力活。

如果要做这样的能力,应该考虑什么呢?

需求分析

这就需要借助Node来实现,需要用到的 fs path http 模块。

实现

1 实现可发布npm包

1

2

3

"bin": {

  "readimg": "./index.js"

},

2 集成到别的项目

1

2

3

4

"scripts": {

  

  "test": "readimg"

},

执行npm run test

就能看到当前项目执行了读取文件的包的代码了。 现在只输出了 111距离读取文件还很远,下面来实现读取文件

3 读取文件

1

2

3

4

5

6

7

8

#!/usr/bin/env node

const init = async () => {

    const readFiles = await getFileFun();

    const html =  await handleHtml(readFiles);

    createServer(html);

}

 

init();

这里解释一下 ,各函数作用

主流程就是这样。

这里读取 test-read-img 的html文件,并替换。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

const fs = require('fs').promises;

const path = require('path');

 

const createImgs = (images=[]) => {

    return images.map(i => {

        return `<div class='img-warp'>

           <div class='img-item'>  <img  src='${i}' /> </div>

           <div class='img-path'>文件路径 <span class='path'>${i}</span></div>

         </div>`

    }).join('');

}

 

async function handleHtml(images=[]) {

    const template =   await fs.readFile(path.join(__dirname,'template.html'),'utf-8')

    const targetHtml = template.replace('%--%',`

     ${createImgs(images)}

    `);

   return targetHtml;

}

 

module.exports = {

 handleHtml

}

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

  const http = require('http');

const fs = require('fs').promises;

const path = require('path');

const open = require('open');

 

const createServer =(html) => {

  http.createServer( async (req,res) => {

      const  fileType = path.extname(req.url);

      let pathName = req.url;

      if(pathName === '/favicon.ico') {

        return;

      }

      let type = ''

      if(fileType === '.html') {

          type=`text/html`

      }

      if(fileType === '.png') {

         type='image/png'

      }

      if(pathName === '/') {

          res.writeHead(200,{

              'content-type':`text/html;charset=utf-8`,

              'access-control-allow-origin':"*"

          })

            res.write(html);

            res.end();

            return

      }

      const data = await fs.readFile('./' + pathName );

      res.writeHead(200,{

          'content-type':`${type};charset=utf-8`,

          'access-control-allow-origin':"*"

      })

      res.write(data);

      res.end();

       

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

   console.log('project is run: http://localhost:3004/')

  open('http://localhost:3004/')

  });

 

  

}

 

module.exports = {

  createServer

}

效果

以上就是实现过程,执行一下 npm run test 就可以看到浏览器执行在http://localhost:3004/, 效果如下:

发布

npm login

npm publish

思考

原文链接:https://juejin.cn/post/7185438521807601721
相关文章
最新更新