본문 바로가기

IT칼럼/JavaScript

ExpressJS 를 이용하여 index.html 파일 view

Use ExpressJS to Deliver HTML Files

 

다음 command를 사용하여 express js 시작하기

server.js index.html 파일 생성

$ mkdir express-sendfile
$ cd express-sendfile
$ npm init
$ npm install express --save
$ touch server.js index.html

server.js

ar express = require('express');
var app = express();
var path = require('path');

// viewed at http://localhost:8080

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(8080);

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample Site</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <style>
        body { padding-top:50px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <h1>res.sendFile() Works!</h1>
        </div>
    </div>
</body>
</html>

Run server.js

$ node server.js

 


references : 

https://scotch.io/tutorials/use-expressjs-to-deliver-html-files

'IT칼럼 > JavaScript' 카테고리의 다른 글

JSX의 개념과 기본 문법 소개  (0) 2019.04.16
Regular Expression  (0) 2019.03.01