* Node.js
JavaScript를 사용하여 서버를 실행할 수 있도록 만든 런타임 환경.
Node.js 예제 (JavaScript ) _ HTTP 서버
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello, World!");
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});
=> Node.js만으로도 서버를 만들 수 있지만, 매우 기본적인 형태.
로직 하나하나 만들어줘야 해서 비효율적.
더 쉽게 웹 서버를 만들 수 있도록 나온게 Express.js (Node.js 기반의 웹 프레임워크)
* Express.js
Node.js에서 웹 서버를 쉽게 만들 수 있도록 도와주는 프레임워크.
Express.js 예제 (JavaScript )
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello, World!");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
=> Node.js만 사용했을 때보다 훨씬 간단하게 웹 서버를 만들 수 있음.
HTTP 요청을 미들웨어 방식으로 처리할 수 있어서 확장성이 좋음.
Node.js에서 가장 많이 사용하는 웹 프레임워크지만, 규모가 커지면 유지보수가 어려울 수 있음.
더 구조화된 대규모 서버 개발을 위해 나온게 NestJS (Express를 기반으로 만든 구조화된 백엔드 프레임워크)
* Nest.js
NestJS는 Express.js를 기반으로 만든 풀스택 백엔드 프레임워크. ( TypeScript 기반)
- Express가 자유도가 높은 반면, NestJS는 아예 구조를 잡아줌
(Spring Boot처럼 컨트롤러, 서비스, 모듈 기반으로 구조화) - Express를 내부적으로 사용하지만, 선택적으로 Fastify 같은 다른 서버도 사용할 수 있음.
NestJS 예제 (TypeScript )
import { Controller, Get } from "@nestjs/common";
@Controller()
export class AppController {
@Get()
getHello(): string {
return "Hello, NestJS!";
}
}
NestJS 예제 (JavaScript)
const { Controller, Get } = require("@nestjs/common");
@Controller()
class AppController {
@Get()
getHello() {
return "Hello, NestJS!";
}
}
=> JavaScript 도 지원은 하지만 TypeScript 권장.
NestJS 서버 실행 코드
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
( Rest API 클라이언트 )
- Postman
- Insomnia
'기타' 카테고리의 다른 글
[LINUX/JAVA] 자바 버전 관리 도구 (alternatives) (0) | 2025.02.11 |
---|---|
[LINUX] 포그라운드/백그라운드 프로세스(nohup, &) (0) | 2025.02.11 |
[Docker] 도커 컨테이너, Dockerfile, Volume, 도커 네트워크 (0) | 2025.02.10 |
[API / Python] Geolocation API_현재 위치좌표 추출 (0) | 2025.02.09 |
[Linux] 표준 출력(stdout) & 표준 에러(stderr) 읽어오기 (0) | 2025.01.27 |