수

nodejs 파일 읽기

· 2019-01-11 (금) 18:25:32 · 2903 · 1

Node.js 파일 시스템 모듈

 

파일 서버로서의 Node.js

 

Node.js 파일 시스템 모듈을 사용하면 컴퓨터의 파일 시스템을 사용하여 작업 할 수 있습니다.

파일 시스템 모듈을 포함하려면 다음 require()방법을 사용하십시오 .

var fs = require('fs');

 

파일 시스템 모듈의 일반적인 사용 :

  • 파일 읽기
  • 파일 만들기
  • 파일 업데이트
  • 파일 삭제
  • 파일 이름 바꾸기

파일 읽기


이 fs.readFile()방법은 컴퓨터의 파일을 읽는 데 사용됩니다.

Node.js와 같은 폴더에 다음 HTML 파일이 있다고 가정합니다.

demofile1.html

<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>

 

 

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);

위의 코드를 "demo_readfile.js"파일에 저장하고 파일을 시작하십시오.

 

demo_readfile.js 시작 :

C:\Users\Your Name>node demo_readfile.js

|

댓글 1개

2019-01-18 (금) 00:13:14
원문 링크도 함께 표시해주시면 안될까요?
게시판에 글이 들어가니 가독성이..
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 19-06-24 조회 3,189
PHP 19-06-09 조회 3,811
MySQL 19-04-01 조회 4,864
jQuery 19-03-20 조회 4,250
node.js 19-02-12 조회 3,351
node.js 19-02-12 조회 3,085
node.js 19-02-12 조회 3,349
node.js 19-02-07 조회 3,326
node.js 19-02-07 조회 3,064
node.js 19-02-07 조회 3,210
node.js 19-02-04 조회 2,948
node.js 19-02-04 조회 2,921
node.js 19-02-04 조회 3,370
node.js 19-01-31 조회 2,479
node.js 19-01-31 조회 2,628
node.js 19-01-31 조회 3,106
node.js 19-01-28 조회 2,868
node.js 19-01-28 조회 2,951
node.js 19-01-28 조회 2,793
node.js 19-01-22 조회 3,124
node.js 19-01-22 조회 2,886
node.js 19-01-22 조회 3,310
node.js 19-01-16 조회 2,754
node.js
[node.js]
19-01-16 조회 2,650
node.js 19-01-16 조회 9,326
node.js 19-01-12 조회 4,404
node.js 19-01-12 조회 3,006
node.js 19-01-12 조회 3,152
node.js 19-01-11 조회 2,696
node.js 19-01-11 조회 4,051