React

[리액트] NODE JS와 EXPRESS JS 다운로드 하기

뉴라코 2024. 11. 23. 17:37

1번) terminal에 node- v 입력해서 버전 뜨는지 확인

-terminal에 cd documents  입력해서 '문서'파일에 들어가기

- mkdir (폴더명) 입력해서 '폴더명' 폴더 만들기

- cd 폴더명 입력해서 '폴더명' 폴더에 들어가기

2번) npm 패키지 만들기 위해서 npm init 입력

- 엔터 엔터 누르면서 넘어가고 폴더명 불러오기

3번) package.json 생성되어 있음. 

4번) index.js라는 파일 만들기

5번) terminal에 npm install express -- save 입력해서 node js의 프레임워크인 express js 다운로드 받기

- package.json에서 express 확인 가능

6번) 위 과정을 하면 알아서 생기는 node_modules

 

마지막 단계 링크

https://expressjs.com/en/starter/hello-world.html

 

Express

Get started with Express.js by building a simple 'Hello World' application, demonstrating the basic setup and server creation for beginners.

expressjs.com


<index.js 파일>

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

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

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

-express 모듈을 가져온다

-function을 이용해서 새로운 express app을 만든다

-5000번 포트를 백서버로 둔다

-app에 루트 디렉토리에 hello world!가 출력되도록 한다.

 

<package.json>

{
  "name": "boiler-plate",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "yujin",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.21.1"
  }
}
 "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

두 번째줄 scripts안에  "start":"node index.js", 를 추가한다.

 terminal에 npm run start를 입력한다. 

http://localhost:5000/ 를 입력하여 들어가면 hello world!라고 출력되는 것을 확인할 수 있다. 

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

왜냐하면 index.js에서 이렇게 출력하라고 했기 때문에!