Javascript/Node.js
NodeJS - Static [ES5]
Back
2018. 10. 7. 16:38
- Static
정적(static) 사이트 | 하나의 html 파일이 하나의 웹페이지를 가지는 사이트.(html 의 변화가 없음) 웹사이트의 route 구조가 파일 디렉토리 구조와 동일. 파일들이 접근 제한이 없음(public) |
동적(dynamic) 사이트 | 하나의 html 파일이 여러개의 웹페이지를 가지는 사이트.(필요에 따라 html 의 내용이 변화됨) 웹사이트의 route 를 제작자가 직접 파일에 연결할 수 있음. router를 통해 개별 파일에 접근 제한을 할 수 있음 |
출처 : https://m.blog.naver.com/PostView.nhn?blogId=azure0777&logNo=220469049820&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F
static으로 지정하면
경로로 부터 안쪽에 있는 파일을 경로로 통해서 가져올수있다.
ex) /public > /public/index.html
이런식으로 불러오는게 가능해진다 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | var http = require('http'), express = require('express'), static = require('serve-static'), path = require('path') ; var app = express(); app.use('/public',static(path.join(__dirname, '../day_09/public'))); app.use(function(req,res,next){ console.log("미들웨어 1"); res.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}); res.write('static 이다 : '+ '<br>'); res.write('<a href="public/login.html">로그인</a>'); res.write('<a href="public/list.html"> 리스트</a>'); res.end(); }); http.createServer(app).listen(3000,function(){ console.log('서버 기동'); }); | cs |
.use('/public',static(path.join(__dirname, '../day_09/public')))
'/public' 패스로 들어오게 되면
__dirname( 이 파일의 디렉토리 네임 ) 곳에
와서 'public'(경로) 으로가서 안에 있는것을 열으란 뜻이다.
static으로 정적으로 지정했기 때문에 /public 디렉토리에 접근이 가능하다.