nodejs基础学习-文件读取
生活随笔
收集整理的這篇文章主要介紹了
nodejs基础学习-文件读取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
同步讀取
var http = require('http'); var fs = require('fs');http.createServer(function(req, res) {res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});if (req.url !== '/favicon.ico') {// 同步讀取文件var data = fs.readFileSync('./test.html', 'utf-8');console.log(data);res.write(data);} }).listen(8000);console.log('server is running at http://localhost:8000'); 復制代碼異步讀取
var http = require('http'); var fs = require('fs');http.createServer(function(req, res) {res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});if (req.url !== '/favicon.ico') {// 異步讀取文件fs.readFile('./test.html', function(err, data) {console.log(data);res.write(data);res.end('哈哈哈');});} }).listen(8000);console.log('server is running at http://localhost:8000'); 復制代碼轉載于:https://juejin.im/post/5ce68f4ef265da1bbd4b4d1b
總結
以上是生活随笔為你收集整理的nodejs基础学习-文件读取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云计算开发技术,Python自动化运维开
- 下一篇: 解决 APP启动白屏黑屏问题