Develop/Node JS

[Node.js] 설치 및 초기설정(express)

팅코벨 2022. 2. 10. 18:10
728x90

node.js 노션도 만드는 중! 블로그에도 열심히.. 옮겨놓도록 하겠어욤🥳 (...라고 100번째 말만하는중ㅎㅎ)

언제까질지는 몰라도 하이팅!

설치 및 초기설정(Express)

node.js 설치

https://nodejs.org/ko/

 

Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

//버전확인이 된다면 성공
$ node -v    // v16.13.2
$ npm -v     // 8.1.2
  • nodemom설치: 실시간으로 스크립트 파일을 디버깅 할 수 있다.
$ npm install --global nodemon
  • 원하는 폴더내에서 폴더 생성
$ mkdir 폴더이름
npm
$ npm init

package.json에서 설정한 내용들 확인 가능

 

express 설치
$ npm install express --save

dependencies 에 설치된 내용이 추가됩니다


시작하기

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

 

Express "Hello World" example

Hello world example Embedded below is essentially the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade

expressjs.com

위의 예시 링크에 있는 예시 코드로 시작해보아요!

//백엔드(서버) 시작페이지
const express = require("express");   //express설치해서 사용가능
const app = express();                //app에 사용할 express함수 불러오기
const port = 5000;  //원하는 포트로(3000,4000,5000)

app.get("/", (req, res) => {
  res.send("Hello World!");
});   //루트 디렉토리에 hello world 뜨도록

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);    //해당 포트에서 실행(리스너)
});

package.json에 “start”에 시작할 app과 페이지 입력

{
  "name": "nodeprac1",
  "version": "1.0.0",
  "description": "prac1",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",    //node앱을 실행 - 시작점이 index.js 
    "test": "echo \\"Error: no test specified\\" && exit 1"
  },
  "author": "arimSeo",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.2"
  }
}
  • npm 명령어로 서버 실행(위의 “start” 부분이 실행됨) → django에서 runserver하는 것과 동일
$ npm run start

5000포트 에러가 뜬다면 다른 3000포트로 실행

 

실행화면
<http://localhost>:포트번호


다음은 MongoDB연결에 대해서 작성하도록 할게요! 많관부

728x90
반응형