티스토리 뷰

정적 페이지

const express = require('express');
const app = express();
const port = 3000;

//public 디렉토리 생성 필요.
//static.html(정적 파일)이 위치할 디렉토리 지정.
app.use(express.static('public'));

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    Hello, Static!
    <ul>
        <li>coding</li>
        <li>coding</li>
        <li>coding</li>
        <li>coding</li>
        <li>coding</li>
    </ul>
</body>
</html>

 

동적 페이지

const express = require('express');
const app = express();
const port = 3000;

app.get('/dynamic', (req, res) => {
  let lis = '';
  
  for(let i = 0; i < 5; i++){
    lis += '<li>coding</li>';
  }
  let time = Date();
  let output = `
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Document</title>
  </head>
  <body>
      Hello, Dynamic!
      <ul>
        ${lis}
      </ul>
      ${time}
  </body>
  </html>`;
  res.send(output);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

 

참고

www.youtube.com/watch?v=z8S_K25ueXI&feature=emb_title

'Framework > Express.js' 카테고리의 다른 글

쿼리스트링(Querystring)  (0) 2021.02.10
Jade : Express 템플릿 엔진  (0) 2021.02.10
Nodejs 동기/비동기  (0) 2021.02.10
Underscore.js  (0) 2021.02.10
프로젝트에 종속적인 모듈 설치  (0) 2021.02.10
댓글
Total
Today
Yesterday
링크
Apple 2023 맥북 프로 14 M3, 스페이스 그레이, M3 8코어, 10코어 GPU, 512GB, 8GB, 한글