ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Node.js] static directory 설정
    컴퓨터/Node.js 2020. 1. 19. 21:31

    * static file

    자바스크립트와 같은 파일을 static file이라고 한다.

    변경되지 않는 정적인 파일

    ex. javascript, css, image file

     

    * app.use(express.static(폴더이름))

     

    static file을 모아놓은 directory를 지정해두고 요청들어오면 바로 주도록 등록한다.

    다음 예제는 directory이름이 'public'인 예시.

    var express = require('express'); // 반환값이 함수형태다.
    var app = express() // express는 함수다
    app.listen(3000, function() {
        // default는 3000포트
        console.log("start! express server on port 3000");
    });
    
    //static이라는 함수에 directory 이름 넣어주면 그 디렉토리를 static으로 기억한다.
    // 이렇게 등록해두면 url routing을 하지 않아도 된다.
    app.use(express.static('public'));

    위와 같이 등록을 해두면

    <script src="main.js"></script> 처럼 바로 쓸 수 있다.

     

    출처 : inflearn, Node.js 웹개발로 알아보는 백엔드 자바스크립트의 이해

    '컴퓨터 > Node.js' 카테고리의 다른 글

    [Node.js] routing tip  (0) 2020.02.10
    [Node.js] CORS 해결  (0) 2020.01.20

    댓글

Designed by Tistory.