Node.js Tutorial – 6 [Express Static file]

본 글에서는 txtdir이라는 디렉터리를 생성하고 그 안에 txt 파일을 생성하였다.

hello.txt를 브라우저에서 request했지만 찾을 수 없다고 뜬다.

정적인 파일을 사용하기 위해 express.static(“디렉터리”)를 use 함수 파라미터에 넣어 적용시켜줘야한다.

app.use(express.static("txtdir"));
const express = require("express");
var app = express();

const port = 3000
const hostname = "127.0.0.1"

app.use(express.static("txtdir"));

app.get("/",function(req,res){
    res.send("LIST");
});

app.get("/upload",function(req,res){
    res.send("UPLOAD");
});

app.get("/delete",function(req,res){
    res.send("DELETE");
});

app.get("/add",function(req,res){
    res.send("ADD");
});


app.listen(port,hostname,function(){
    console.log(`Server Run ${hostname}:${port}`);
});

static 폴더를 등록한 후 서버를 시작하면 적요된 것을 볼 수 있다.

Leave a Comment