├── README.md ├── package.json ├── .gitignore └── app.js /README.md: -------------------------------------------------------------------------------- 1 | # 静态资源服务器 2 | 3 | ## 使用步骤 4 | 5 | ``` 6 | 克隆到本地 7 | 使用npm 安装依赖(包) 8 | node app.js即可运行 9 | 静态资源放在 www目录下面 10 | ``` 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "01.staticserver", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "dependencies": { 7 | "mime": "^2.3.1" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "", 14 | "license": "ISC" 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | /* 2 | 静态资源服务器 3 | 让电脑有类似于Apache的功能 4 | 有一个www目录 把自己写好的静态网站 丢进去 5 | 开启之后 用户 通过ip地址访问 6 | 有首页 返回 页面给用户 7 | 如果没有首页 列表给用户 8 | */ 9 | 10 | // nodejs 要开启 http服务 导入模块 生成路径 读取文件(前端开发中 快速操纵dom ->jQ 快速生成轮播图 ->swiper 使用ui框架 ->bs) 11 | let http = require('http'); 12 | let fs = require('fs'); 13 | let path = require('path'); 14 | 15 | // 导入mime 16 | let mime = require('mime'); 17 | // mime.getType('txt'); mime 导入的第三方包 .getType 包里面的功能 18 | // $.ajax() 19 | 20 | // 网站根目录的 绝对路径 21 | let rootPath = path.join(__dirname,'www'); 22 | 23 | // 开启服务 24 | http.createServer((request,response)=>{ 25 | 26 | // 有请求过来(用户访问时触发) 27 | // console.log('你来啦'); 28 | 29 | // 获取用户需要获取的文件->路径 30 | let reqUrl = request.url; 31 | // console.log(reqUrl); 32 | let pathFile = path.join(rootPath,reqUrl); 33 | console.log('请求的路径是',pathFile); 34 | // 判断文件是否存在 35 | if(fs.existsSync(pathFile)){ 36 | // 存在-> 37 | // 是否是文件夹(读取文件夹的函数) 38 | if(pathFile[pathFile.length-1]=='\\'){ 39 | console.log(pathFile,'是文件夹'); 40 | }else{ 41 | console.log(pathFile,'是文件'); 42 | // 读取文件 返回读取的文件 43 | fs.readFile(pathFile,(err,data)=>{ 44 | console.log(pathFile,'读取文件完毕 返回'); 45 | // 自行判断 后缀名(.js .css .html .jpg .png .gif .ico) 46 | // mime类型 47 | // if else if else 48 | // 查找是否有人实现了 类似的功能 49 | response.writeHead(200,{ 50 | 'content-type':mime.getType(pathFile) 51 | }) 52 | 53 | if(err){ 54 | console.log(err); 55 | }else{ 56 | response.end(data); 57 | } 58 | }) 59 | } 60 | // 是文件夹(列表) 61 | // 不是文件夹(读取文件 根据类型 设置不同的 content-type 返回读取的内容) 62 | }else{ 63 | // 不存在-> 64 | // 404 提示用户 not find 65 | response.writeHead(404,{ 66 | 'content-type':"text/html;charset=utf-8" 67 | }) 68 | response.end(` 69 |
哥们,你找的页面木有哦, 你再看看吧 O(∩_∩)O哈哈~
71 | `) 72 | } 73 | 74 | 75 | // 响应内容 76 | // response.end('you come'); 77 | }).listen(80,'127.0.0.1',()=>{ 78 | console.log('listen to 127.0.0.1:80 success'); 79 | }) --------------------------------------------------------------------------------