• node.js入门(二)文件系统


    服务端对文件进行读、写是很常用的功能,那么怎么实现读、写?

    使用fs模块,fs模块又两个方法readFile()和writeFile(),两个方法就是对文件进行读、写的

    1、读取文件使用readFile(fileName,callback)方法,该方法接受两个参数,fileName:需要读取的文件名;callback:读取的回调函数,回调函数有两个参数 分别为err读取错误,和data读取到的内容

    在项目根目录新建一个index.txt,并写入一些内容

    const http = require("http")
    const fs = require("fs")
    var server = http.createServer(function (req,res) {
        res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
        // 异步读取
        fs.readFile('./index.txt',function (err,data) {
            if(err){
                res.write("文件不存在")
            }else {
                res.write(data)
            }
            res.end()
        })
        // 同步读取
        var data = fs.readFileSync('index.txt');
        console.log("同步读取: " + data.toString());
    })
    server.listen(8080)

    启动服务器,浏览器访问,可以看到浏览器输入了文件的内容

    如果使用console在命令工具中打印输出的是buffer,这是原始的二进制数据,服务器除了要出来文本,还要处理图片等二进制文件

    如果想要看到文字就要使用data.toString()

    2、对文件写入内容使用writeFile(fileName,data[,option],callback)方法

    fileName:为需要写入的文件;

    data:为写入的内容;

    callback:写入的回调函数;

    如果options是一个字符串,则他指定字符编码

    const http = require("http")
    const fs = require("fs")
    var server = http.createServer(function (req,res) {
        res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
        fs.writeFile('./index.txt','我是通过node.js的文件系统fs.writeFile写入文件的内容','utf8',function (err) {
            // 异步读取
            fs.readFile('./ceshi.txt',function (err,data) {
                if(err){
                    res.write("文件不存在")
                }else {
                    res.write(data)
                }
                res.end()
            })
        })
    })
    server.listen(8080)
    

    打开index.txt文件可以看到内容已经写入了,但是如果index.txt原本就有内容,再写入,那么原来的内容将被新写入的内容替代

    更多文件操作可以去官网查看

    在项目根目录新建一个www文件夹,在文件新建a.html、b.html

    a.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            div{
                 300px;
                height: 300px;
                margin: 0 auto;
                background-color: aqua;
            }
            p{
                text-align: center;
                color: red;
                font-size: 30px;
            }
        </style>
    </head>
    <body>
    <div>
        <p>a.html</p>
    </div>
    </body>
    </html>
    

    b.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            div{
                 800px;
                height: 300px;
                margin: 0 auto;
                background-color: burlywood;
            }
            p{
                text-align: center;
                color: red;
                font-size: 30px;
            }
        </style>
    </head>
    <body>
    <div>
        <p>这是b.html页面</p>
    </div>
    </body>
    </html>
    

    server.js

    const http = require("http")
    const fs = require("fs")
    var server = http.createServer(function (req, res) {
        var fileName = './www' + req.url
        fs.readFile(fileName, function (err, data) {
            console.log("bbbbbbb")
            if (err) {
                res.write("404")
            } else {
                res.write(data)
            }
            res.end() 
        })
        console.log("aaaaaa")
    })
    server.listen(8080)

    启动服务,浏览器访问localhost:8080/a.html 和 localhost:8080/b.html 可以看到确实按照html的样式显示了

    注意打印的内容顺序,res.end()的位置

  • 相关阅读:
    架构之美阅读笔记05
    架构之美阅读笔记04
    已经导入到VS工具箱中的DevExpress如何使用
    C#中遇到的方法总结
    vs下C# WinForm 解决方案里面生成的文件都是什么作用?干什么的?
    ssh关于含有外键的传值中无法识别正确的action的原因和解决办法
    MVC模式在Java Web应用程序中的实例分析
    浅谈对MVC的理解
    简述23种设计模式
    浅谈对可用性和易用性的认识以及对如何增加系统功能的理解
  • 原文地址:https://www.cnblogs.com/YAN-HUA/p/10813504.html
Copyright © 2020-2023  润新知