nodejs_100个实例(3)_文件读取

nodejs_100个实例(3)_文件读取

一、效果

二、技术

1、基本模块:fs、http

三、思路

http创建服务器,在服务器内fs进行文件读取,将文件内容展示在前端。localhost:3000

四、源码

 1 var fs=require('fs');
 2 var http=require('http');
 3 http.createServer(function(req,res){
 4     fs.readFile('txt.txt',function(err,data){
 5         if(err){
 6             console.log(err);
 7         }
 8         else{
 9             res.writeHead(200,{
10             'Content-Type':'text/html;charset=utf8',
11             });
12 
13             res.write(data);
14 
15 
16         }
17     })
18     
19 }).listen(3000);
View Code
原文地址:https://www.cnblogs.com/carsonwuu/p/8700426.html