zoukankan      html  css  js  c++  java
  • 文件内容作为服务器的响应练习

     1 /**
     2  * 创建一个服务
     3  * GET  /index.html     响应当前目录下 public 目录中 index.html 文件内容
     4  * GET  /css/app.css    响应当前目录下 public 目录中 css/app.css 文件内容
     5  * GET  /js/app.js    响应当前目录下 public 目录中 js/app.js 文件内容
     6  */
     7 
     8  //获取fs文件模块
     9  const fs= require('fs');
    10  //获取将请求的url模块
    11 
    12  const urltool= require('url');
    13 
    14  //创建服务
    15 require('http').createServer((request, response)=>{
    16     // 获取请求方式
    17     let method= request.method.toUpperCase();
    18     //获取url路径
    19     let pathname= urltool.parse(request.url).pathname;
    20     // 看我打印结果
    21     console.log(pathname)
    22  // /css/app.css拿着这个路径进入下面判断
    23     //判断请求方式和路径 /public/css/app.css 不满足条件,
    24     // 所有条件都不满足.也就是都没有进入任何一个判断,没进入判断,就没有相应内容,外面就会一直在等待
    25     // 这样的话自然就读不到内容
    26    
    27     if(method==='GET' && pathname === '/index.html'){
    28         const fd = fs.createReadStream(__dirname+'\\public\\index.html')
    29         fd.pipe(response)  
    30         // fs.readFile(__dirname+'\\public\\index.html', (err, data)=>{
    31         //     if(err){
    32         //         //返回状态吗
    33         //         response.statusCode=404;
    34         //         response.end('404')
    35         //         return;
    36         //     }
    37 
    38         //     response.end(data)
    39         // });
    40 
    41         ///css/app.css
    42     }else if(method==='GET' && pathname==='/app.css'){
    43         const fd = fs.createReadStream(`${__dirname}\\public\\css\\app.css`)
    44         fd.pipe(response) 
    45         // fs.readFile(`${__dirname}\\public\\css\\app.css`, (err, data)=>{
    46         //     if(err){
    47         //         //返回状态吗
    48         //         response.statusCode=404;
    49         //         response.end('404')
    50         //         return;
    51         //     }
    52 
    53         //     response.end(data)
    54         // });
    55     }else if(method==='GET' && pathname==='/app.js'){
    56         //创建读取流
    57         const fd = fs.createReadStream(`${__dirname}\\public\\js\\app.js`)
    58         fd.pipe(response)
    59         // fs.readFile(`${__dirname}\\public\\js\\app.js`, (err,data)=>{
    60         //     if(err){
    61         //         //返回状态吗
    62         //         response.statusCode=404;
    63         //         response.end('404')
    64         //         return;
    65         //     }
    66 
    67         //     response.end(data)
    68         // })
    69     }else{
    70         response.setHeader('Content-type','text/plain;charset=utf-8');
    71         response.end('你的路径输入不正确')
    72     }
    73     
    74 
    75 }).listen(8000, ()=>{
    76     console.log('node服务开启了')
    77 });

    2.升级版,对文件路径和报文的url关联,不用每次判断路径

     1 // static 静态. 通过 URL 访问文件夹中的静态资源
     2 // 静态资源  长时间内容不发生改变的资源
     3 // HTML  CSS  JS  图片  字体文件 音频 视频  属于静态资源
     4 
     5 /**
     6  * GET   /index.html     返回 public/index.html 的文件内容
     7  * GET   /css/app.css     返回 public/css/app.css 的文件内容
     8  * GET   /js/app.js     返回 public/js/app.js 的文件内容
     9  */
    10 const fs = require("fs");
    11 const http = require("http");
    12 const urlTool = require("url");
    13 
    14 const server = http.createServer((request, response) => {
    15     //提取参数 get  Get
    16     let method = request.method.toUpperCase();
    17     let pathname = urlTool.parse(request.url).pathname; //将request.url转换成对象,获取pathname
    18 
    19     // console.log(pathname);//  /index.html     =>    public/index.html
    20                          //   /css/app.css    =>    public/css/app.css
    21     //拼接文件路径
    22     let filePath = __dirname + '/public'+pathname;
    23     // let filePath = __dirname + '\\public' + pathname.replace(/\//g,"\\");
    24     
    25     //读取文件的内容fs.existsSync(filePath),判断文件路径是否存在
    26     if(fs.existsSync(filePath) && method === "GET"){
    27         //读取对应的文件内容
    28         fs.readFile(filePath, (err, data)=>{
    29             if(err){  //如果是存在的文件夹,就会报错。readFile方法是操作文件的
    30                 //如果读取出错
    31                 response.statusCode = 500;
    32                 response.end("<h1>500 Internal Server Error</h1>");
    33                 return;
    34             }
    35             response.end(data);
    36         });
    37     }else{
    38         //如果文件不存在
    39         response.statusCode = 404;
    40         response.end("<h1>404 Not Found</h1>")
    41     }
    42 });
    43 
    44 server.listen(8000, () => {
    45     console.log("service running, 8000 端口监听中......");
    46 })
  • 相关阅读:
    Core 1.0中的管道-中间件模式
    java平台的常用资源
    C#设备处理类操作
    C#语音录制
    Web中的性能优化
    nginx+lua+redis构建高并发应用(转)
    HttpLuaModule——翻译(Nginx API for Lua) (转)
    Nginx各版本的区别
    Linux(Centos)中tcpdump参数用法详解(转)
    我见过最好的vsftpd配置教程(转)
  • 原文地址:https://www.cnblogs.com/fsg6/p/13081903.html
Copyright © 2011-2022 走看看