[React JS] 4. ๋ฐฑ์—”๋“œ ๊ธฐ๋ณธ๊ตฌ์กฐ ์ƒ์„ฑํ•˜๊ธฐ

 

1. ๋ฐฑ์—”๋“œ ๊ธฐ๋ณธ๊ตฌ์กฐ ์ƒ์„ฑ

package.json ์ƒ์„ฑ

npm init

express ์„ค์น˜

npm install express

- index.js ๊ฐ€ entryํŒŒ์ผ (์—ฌ๊ธฐ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ด)

 

nodemon ์„ค์น˜

npm install nodemon

ํŒŒ์ผ๊ตฌ์กฐ ์ƒ์„ฑ

 

ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ๋‹ค์šด

npm install bcryptjs cors dotenv express jsonwebtoken mongoose

- bcrypt : ๋น„๋ฒˆ ์•”ํ˜ธํ™”

- cors : origin ๋‘ ๊ฐœ์—์„œ ํ†ต์‹  ๊ฐ€๋Šฅํ•˜๊ฒŒ

- env : ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ƒ์„ฑ ์ƒ์„ฑํ•œ๊ฑฐ ์‚ฌ์šฉ

- jsonwebtoken : ์‚ฌ์šฉ๊ฐ€๋Šฅ

- mongoose : mongo db ์‚ฌ์šฉ


2. express.static()

์ด๋ฏธ์ง€, css, js ํŒŒ์ผ๊ฐ™์€ ์ •์ ์ธ ํŒŒ์ผ ์ œ๊ณตํ•˜๋ ค๋ฉด Express์˜ express.static ๋‚ด์žฅ ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ

app.use(express.static('uploads'));
// ๊ฐ€์ƒ๊ฒฝ๋กœ ์ง€์ •๋„ ๊ฐ€๋Šฅ
// ex) app.use('/image', express.static('๊ฒฝ๋กœ'));

์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๋‚จ -> ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ํ•ด์•ผํ•จ

app.use(express.static(path.join(__dirname, "uploads")));

3. CORS

- server์™€ client ํ†ต์‹  --> ๊ทธ๋ƒฅ request ๋ณด๋‚ด๊ธฐ ๋ถˆ๊ฐ€

- Same Origin Policy (SOP) ์ •์ฑ… (๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ Origin์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ฒ„ request ๋ถˆ๊ฐ€)

- ํ•ด๊ฒฐ) Cross-Origin Resource Sharing (CORS)


4. Express.json()

- ๋ฏธ๋“ค์›จ์–ด

app.post('/', (req, res) => {
  console.log(req.body);
  res.json(req.body);
}

- json response๊ฐ’ ๋ฐ›์•„์˜ค์ง€ ๋ชปํ•จ

app.use(express.json());

- ๋“ฑ๋กํ•˜๋ฉด ๊ฐ€๋Šฅ


5. MongoDB

- mongoDB ํšŒ์›๊ฐ€์ž…

- mongoose ์„ค์น˜

npm install mongoose
const dotenv = require("dotenv");
const mongoose = require("mongoose");

dotenv.config();

mongoose
  .connect(process.env.MONGO_URI)
  .then(() => console.log("MongoDB Connected..."))
  .catch((err) => console.log(err));

6. Mongoose๋ž€

- ๋ชฝ๊ณ  DB๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ

- ๋ชฝ๊ตฌ์Šค๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„  Schema ํ•„์š”

- ํ•ด๋‹น Schema๋กœ ๋ชจ๋ธ์„ ๋งŒ๋“ฆ

- ์„ ํƒ์‚ฌํ•ญ์ž„

๋ชฝ๊ตฌ์Šค ์‚ฌ์šฉ๋ฐฉ๋ฒ•

- ์Šคํ‚ค๋งˆ ์ƒ์„ฑ

- ์Šคํ‚ค๋งˆ ์ด์šฉํ•ด์„œ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธฐ

- ๋ชจ๋ธ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ์ €์žฅ/์—…๋ฐ์ดํŠธ/์‚ญ์ œ ์ž‘์—… ๊ฐ€๋Šฅ

 

์Šคํ‚ค๋งˆ์ƒ์„ฑ

const userSchema = mongoose.Schema({
  name: {
    type: String,
    maxlength: 50,
  },
  email: {
    type: String,
    trim: true,
    unique: true,
  },
  password: {
    type: String,
    minlength: 5,
    maxlength: 90,
  },
  role: {
    type: Number,
    default: 0,
  },
  image: String,
});

์Šคํ‚ค๋งˆ ์ด์šฉํ•ด์„œ ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ

const User = mongoose.model("User", userSchema);
module.exports = { User };

7. Express์—์„œ ์—๋Ÿฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ

- ์—๋Ÿฌ์ฒ˜๋ฆฌ๊ธฐ

// ์—๋Ÿฌ ๋˜์ง€๋Š” ๋ฒ•
app.get('/', (req, res) => {
  thorw new Error('it is an error');
})
app.use((error, req, res, next) => {
  res.send(error.message || '์„œ๋ฒ„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค.');
})

* ๋น„๋™๊ธฐ ์š”์ฒญ์œผ๋กœ ์ธํ•œ ์—๋Ÿฌ๋Š” ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ธฐ์—์„œ ํ•ด๋‹น ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๊ฐ€ crash ๋จ

* ๋น„๋™๊ธฐ๋กœ ๋งŒ๋“œ๋Š” ๋ฒ• (next ์ด์šฉ)

app.get('/', function(req, res, next) {
  // Reporting async errors *must* go through 'next()'
  setImmediate(() => { next() new Error('it is an error') });
})

 res.status()

app.use((error, req, res, next) => {
  // status ์ฝ”๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌ, ์—†์œผ๋ฉด 500 ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌ 
  res.status(err.status || 500);
})
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ