client 폴더에서 다음 명령어를 실행한다.
작성한 react 코드들을 배포 버전으로 만들어주는 과정이다.
$ npm run build
client와 같은 위치에 server 폴더를 생성한다.
server 폴더 아래에 index.js를 생성한 뒤 다음과 같이 적는다.
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "../client/build")));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "/client/build/index.html"));
});
app.listen(port, () => {
console.log("Server started...");
});
$ npm install nodemon
package.json에
"dev": "nodemon index.js",
을 추가한다.
이제 npm run dev라는 명령어로 서버를 시작할 수 있다.
서버를 시작한 뒤 http://localhost:3000/로 접속했을 때
이 화면이 뜨면 성공이다.