Nodejs Express 초기 설정

박슬빈·2021년 9월 15일
0

프로젝트 생성하기

루트 디렉토리에 아래 명령어를 실행
npm init -y
실행 하면 폴더에 package.json 파일이 생성이됨

express를 사용하기 위해
npm install --save express
을 터미널에 실행

server.js 파일 생성

server.js 파일을 생성

const express = require('express'); // express를 사용한다는 뜻
const app = express(); 
const port = process.envPORT || 8000; //process.env -> nodejs에서 환경 변수를 가져올 때 사용된다.

app.get('/', (req, res) => {
    res.json({
        success: true,
    });
})

app.listen(port, () => {
    console.log('server starting 8000');
});

REST API 의 종류중 하나인 get을 사용함

npm start
를 실행하고
http://localhost:8000/ 를 들어가면

다음과 같은 화면이 보이면 성공한것이다.

profile
이것저것합니다

0개의 댓글