node.js로 server 개발 프로젝트 생성 절차

프로젝트 폴더를 만들어 VS.Code에서 오픈 후 package.json 파일 생성을 위해 터미널에서 다음 명령을 실행한 뒤에 생성된 파일을 편집함

npm init -y

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"name": "tstServer",
"version": "1.0.0",
"description": "",
"keywords": [],
"author": "",
"license": "ISC"
}
{ "name": "tstServer", "version": "1.0.0", "description": "", "keywords": [], "author": "", "license": "ISC" }
{
  "name": "tstServer",
  "version": "1.0.0",
  "description": "",
  "keywords": [],
  "author": "",
  "license": "ISC"
}

nodemon 설치를 위해 다음 명령 실행

npm i nodemon -D

babel.config.json 파일과 nodemon.json 파일을 생성하고 각각 다음 내용으로 입력

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"presets": [ "@babel/preset-env" ]
}
{ "presets": [ "@babel/preset-env" ] }
{
    "presets": [ "@babel/preset-env" ]
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"exec": "babel-node src/index.js"
}
{ "exec": "babel-node src/index.js" }
{
    "exec": "babel-node src/index.js"
}

babel 설치를 위해 다음 명령 실행

npm i @babel/core @babel/cli @babel/node @babel/preset-env -D

express 설치를 위해 다음 명령 실행

npm i express

필요할 경우 pug 설치를 위해 다음 명령 실행

npm i pug

package.json에 설치된 항목에 대한 종속성(Dependency)가 존재하는지 확인하고 다음처럼 scripts 속성을 추가(또는 변경)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"name": "tstServer",
..
"scripts": {
"dev": "nodemon"
},
"devDependencies": {
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.7",
"@babel/node": "^7.20.7",
"@babel/preset-env": "^7.20.2",
"nodemon": "^2.0.20"
},
"dependencies": {
"express": "^4.18.2",
"pug": "^3.0.2"
}
}
{ "name": "tstServer", .. "scripts": { "dev": "nodemon" }, "devDependencies": { "@babel/cli": "^7.20.7", "@babel/core": "^7.20.7", "@babel/node": "^7.20.7", "@babel/preset-env": "^7.20.2", "nodemon": "^2.0.20" }, "dependencies": { "express": "^4.18.2", "pug": "^3.0.2" } }
{
  "name": "tstServer",

  ..

  "scripts": {
    "dev": "nodemon"
  },

  "devDependencies": {
    "@babel/cli": "^7.20.7",
    "@babel/core": "^7.20.7",
    "@babel/node": "^7.20.7",
    "@babel/preset-env": "^7.20.2",
    "nodemon": "^2.0.20"
  },
  "dependencies": {
    "express": "^4.18.2",
    "pug": "^3.0.2"
  }
}

src 폴더 만들어 주고 index.js 파일 생성하고 다음처럼 입력

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import express from "express";
const app = express();
const port = 7777;
const handleListen = () => console.log(`Listening on http://localhost:${port}`)
app.listen(port, handleListen);
import express from "express"; const app = express(); const port = 7777; const handleListen = () => console.log(`Listening on http://localhost:${port}`) app.listen(port, handleListen);
import express from "express";

const app = express();


const port = 7777;
const handleListen = () => console.log(`Listening on http://localhost:${port}`)
app.listen(port, handleListen);

다음 명령을 실행하여 서버 실행하고 웹브라우저에서 http://localhost:7777/로 접속해서 확인

npm run dev

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다