Nodejs实现的一个静态服务器实例

2020-06-17 06:06:08易采站长站整理

                        if (matched && acceptEncoding.match(/bgzipb/)) {
                            response.writeHead(200, “Ok”, {‘Content-Encoding’: ‘gzip’});
                            raw.pipe(zlib.createGzip()).pipe(response);
                        } else if (matched && acceptEncoding.match(/bdeflateb/)) {
                            response.writeHead(200, “Ok”, {‘Content-Encoding’: ‘deflate’});
                            raw.pipe(zlib.createDeflate()).pipe(response);
                        } else {
                            response.writeHead(200, “Ok”);
                            raw.pipe(response);
                        }
                    }
                }
            }
        });

    }
    pathHandle(realPath);
});
server.listen(port);
console.log(“http server run in port:”+port);

首先需要在JS文件里创建一个assets的文件夹,里面放入你要浏览的静态文件,比如,index.html,demo.js等。

运行方式为:在命令行里切换到上面的JS的文件目录,然后输入 node JS文件名

浏览器内输入http://localhost:3333/就会看到效果。

–补上上面代码里缺少的两个模块

mime.js


exports.types = {

  “css”: “text/css”,