웹 서버는 웹애플리케이션에 사용자가 접근 가능하도록 사이트의 모든 정보 (파일들)를 담고 있습니다. 해당 정보요청과 요청에 대한 응답은 HTTP(S)로 많이 이루어지는데요. 오늘은 웹 애플리케이션 프레임워크인 NPM express를 사용하여 Node.js 웹 서버를 만들어보겠습니다.
익스프레스?
익스프레스 (express)는 웹 애플리케이션 제작에 필요한 서버 측의 다양한 기능들을 미리 구현해 놓은 프레임워크로 웹 애플리케이션 제작에 필수기능들을 직접 코드작성 없이 사용하게 해 줍니다.
프로젝트 생성
먼저 npm 패키지를 실행하기 위해 아래 명령어를 실행합니다.
npm init
설정할 때 아래 그림처럼 물어보는 게 많은데 아래와 같이 옵션을 추가하면 자동으로 모든 설정이 완료됩니다 (이후 package.json파일에서 수정가능).
npm init -y
서버코드가 들어갈 .js 파일을 생성합니다 (주로 server.js, app.js라는 파일명을 많이 사용합니다).
express 설치
npm 패키지 중의 하나인 express를 설치합니다.
npm i express
설치가 완료되면 아래와 같이 패키지 정보를 담고 있는 'node_modules' 파일과 dependency의 버전 정보를 담고 있는 package-lock.json파일이 생성됩니다.
서버 만들기
먼저 express 패키지를 가져오고
const express = require('express')
해당 패키지의 기능을 아래처럼 변수에 저장하면 사용준비가 완료됩니다.
const app = express()
서버로 사용할 포트를 지정하고
app.listen(3000)
노드제이에스를 실행하면
node index.js
활성화된 서버확인이 가능합니다.
소스코드
https://github.com/jin-co/web-javascript/tree/master/nodeJS/item_api
GitHub - jin-co/web-javascript: remote storage maneuver practice
remote storage maneuver practice. Contribute to jin-co/web-javascript development by creating an account on GitHub.
github.com
※ 서버를 지정하지 않으면 아래처럼 접근이 불가합니다.
이상으로 express를 사용하여 Node.js 서버를 만드는 방법을 살펴보았습니다.
참고
What is Express.js? | Why should use Express.js? | Features of Express.js (besanttechnologies.com)
What is Express.js? | Why should use Express.js? | Features of Express.js
What is Express.js? Express.js is a free and open-source web application framework for Node.js. It is used for designing and building web applications quickly and easily. Web applications are web apps that you can run on a web browser. Since Express.js onl
www.besanttechnologies.com
express
Fast, unopinionated, minimalist web framework. Latest version: 4.18.2, last published: 4 months ago. Start using express in your project by running `npm i express`. There are 68404 other projects in the npm registry using express.
www.npmjs.com
Introduction to Express - GeeksforGeeks
Introduction to Express - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'백엔드 > 노드제이에스' 카테고리의 다른 글
파일업로드 (0) | 2023.05.22 |
---|---|
노드제이에스 서버 만들기 (8) | 2023.02.20 |
노드제이에스? (8) | 2023.02.12 |
몽고디비 (MongoDB) 와 연결하기 (0) | 2023.01.23 |
노드제이에스 - 앵귤러 서버 API 배포하기 (Render) (0) | 2023.01.22 |