- 파일 생성 = ex) server 명칭으로 파일 생성
- 파일 생성후 node 초기화 작업 = npm init -y
- server 파일에 index.js 파일 생성
3-1. index.js 파일에 express 코드 작성 (초기 서버 생성 및 요청&응답)
const express = require('express')
const app = express()
const port = 3000
app.listen(port, () => {
console.log(`Example app listening http://localhost:${port}`)
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
- pakage.json 파일의 script 에
"start": "node index.js"
코드를 추가하여 사용할 명령어를 추가하여 index.js 파일을 실행 시킬 명령어 입력
- 파일 수정 및 설명
const path = require('path')
const express = require('express');
const app = express();
const port = 8000;
app.use(express.static(path.join(__dirname, "../client/build")))
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../client/build/index.html'))
});
- 리액트에서 구성한 프로젝트를 html로 압축하여 사용하기 위해 React 파일 에서
npm run-script build
로 파일을 빌드 한다.