0
아래 코드는 모두 서버가 시작될 때 index.html at localhost : 3000 /에 제공됩니다. 그런데 왜 사람이 app.get
이상 express.static
를 선택할 것 app.get
express.static 미들웨어 사용
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.get('/',(req,res) => {
res.sendFile(publicPath + '/index.html');
})
app.listen(PORT,() => {
console.log(`Server is running on port ${PORT}`);
})
의 express.static
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.use(express.static(publicPath));
app.listen(PORT,() => {
console.log(`Server is running on port ${PORT}`);
})
이용
사용은 정적 HTML 파일을 제공합니다. (CSS를로) index.html을 다른 정적 파일을 포함하더라도, index.html을하지 않는 다른 정적 페이지를 제공 할 때 express.static will
를 사용하지 않는 코드가 실패 명시
왜 실패할까요? 어떤 특별한 이유? –
sendfile 라인으로 무엇을하고 있는지 Express는 클라이언트가 '/home.html'과 같이 '/'방향을 요청할 때 /index.html을 제공합니다. ExpressJS (실제로 Node.JS)는 어디에서 정보를 찾지 못 했으므로 어디에서 찾을 수 있는지 알려주지 않습니다. 'app.use (express.static (publicPath)) '를 사용하면 Express가 publicPath에서 클라이언트가 요청한 정적 파일을 찾아야한다고 알려줍니다. /index.html/에 css가 포함되어 있다면, 또한 제공되어야하며 Express에 알려주지 않았 으면합니다 (express.static 행으로 지정). – Jibril