본문 바로가기
Back-end/Node.js

[express]express.js 설치및 사용법

by -제이리 2022. 9. 2.
728x90
320x100

https://expressjs.com/

 

express란? 

Express.js는 Node.js에서 HTTP와 관련된 컴포넌트를 기반으로 하는 웹 애플리케이션 프레임워크입니다.


express.js 설치

[준비사항]

1. Node.js 설치가 되어있어야 한다.

2. npm init 명령어로 패키지를 생성해준다.

 

[설치]

위의 사항이 충족이 되면 터미널에서 설치를 해준다.

npm install express

express 실행 - Hello Word

 

 

1. index.js에 웹요청을 위한 코드를 작성한다.

 

index.js

const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
  res.send("Hello World!");
});

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

2. package.json에 start script 추가

package.jscon의 scripts부분에 start를 추가해주면 터미널에서 npm run start를 실행할 수 있다.

시작점은 적어준 index.js가 된다.

 

package.json

{
  "name": "boiler-plate",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js", // 추가를 해준다.
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "yoon",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1"
  }
}

3. npm run start로 실행 후 웹브라우저에서 http://localhost:3000/로 들어가면 hello world가 나오는것을 볼 수 있다.

728x90
320x100

댓글