관리 메뉴

흰둥씨의 개발장

[오늘의 node.js & npm] node 설정 본문

[오늘의 공부]/Node.js & package manager

[오늘의 node.js & npm] node 설정

돈워리비해삐 2023. 3. 5. 01:44

1. 프로젝트 폴더 생성

2. 내부에 react, node 폴더 생성



Express 서버 세팅

node 폴더

// package.json 생성. -y는 모든 설정을 기본으로
npm init -y
// express설치
npm i express

 

node > index.js

웹서버 구동을 5000번 포트로 응답받도록 설정 (맥은 5500으로 지정)

const express = require('express');
const app = express();
const port = 5000;

app.get('/', (req, res) => {
  res.send('Hello World2');
})

app.listen(port, () => {
  console.log(`Server app listening on port ${port}`);
})
node index.js

node > package.json

node index.js로 시작하는 대신 npm start 명령어로 시작되게 변경


{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  }
}​

웹서버 시작(서버 개발내용이 변경될 때마다 재실행 필요, 어디 이름 하나 고치면 서버 띄운거 종료하고 다시 시작하고 해야 결과확인가능)

npm start

 

서버 변경점 인지해서 자동 실행되게 설정하려면 노드몬을 깔아보자~

npm i nodemon --save

이후에 자동 실행되게 하려면 노드몬으로 프로젝트를 돌리면됨 

node <프로젝트경로>//이렇게 시작했다면,
nodemon <프로젝트경로>//요렇게 시작하면 
//내가 개발하면서 변경된 사항을 터미널에서 계속 구동해주지 않아도 
//브라우저 새로고침만 하면 변경된거 확인가능


package.json

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "nodemon": "^2.0.20"
  }
}