Nodejs入门基础(使用fs创建文件夹及文件并获取信息返回页面)

测试直接ping直接设置的端口号
http://localhost:端口号



参考代码:

var fs = require("fs");
var http = require("http");
var url = require("url");

http.createServer(function(req,res){
    if (req.url=="/favicon.ico"){
        return;
    }
    res.writeHead(200,{"Content-type":"text/html;charset=UTF-8"})
    fs.mkdir("./hello",function(err){
        if (err){
            console.log(err);
        }else{
            fs.writeFile("hello/img1.txt","课工场",function(err){
                if (err){
                    console.log(err);
                } else{
                    console.log("创建文件并写入成功!");
                    fs.readFile("./hello/img1.txt",(err,data)=>{
                        if (err){
                            console.log(err);
                        }else{
                            res.end(data);
                        }
                    });
                }
            })
        }
    });
}).listen(3000,"localhost");

猜你喜欢

转载自blog.csdn.net/JayVergil/article/details/83246907