Express로 여러 프로젝트 배포하기

Ritslurrrr·2024년 12월 18일
0

Back-End

목록 보기
2/2

루트 디렉토리 생성

mkdir [ dirName ]
cd [ dirName ]

서버 코드 작성

// server.js

const express = require('express');
const path = require('path');
const fs = require("fs");

const app = express();
const PORT = 3000;


const testDir = path.join(__dirname, 'test');

app.use(express.static("${rootDirName}"));
app.use('/test', express.static(testDir));
app.use('/react', express.static(path.join(__dirname, 'react/build')));


// Root
app.get("/", (req, res) => {
  res.send('Root Route')
});

// HTML PROJECT
app.get('/test', (req, res) => {
  res.sendFile(path.join(testDir, 'index.html'));
});

// REACT PROJECT
app.get('/react', (req, res) => {
  res.sendFile(path.join(__dirname, 'react', 'build', 'index.html'));
})


app.listen(PORT,"0.0.0.0", () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

프로젝트 생성

root/react
root/test
profile
FullStackDeveloper(WEB,APP)

0개의 댓글