gengminy
갱미니의 코딩일지
gengminy
전체 방문자
오늘
어제
  • 분류 전체보기 (61)
    • 🚀 프로젝트 (16)
      • 🎸 고스락 티켓 (4)
      • 🌞 내친소 (5)
      • 🥁 두둥 (7)
    • 📡 백엔드 (31)
      • 🌱 Spring Boot (13)
      • 🐱 Nest.js (10)
      • ⭐ Node.js (8)
    • 🏭 Infra (11)
      • ⚙ 준비를 위한 준비 (2)
      • 🥑 AWS (3)
      • 🐳 Docker (3)
      • ⚡ Github Actions (3)
    • 🌊 프론트엔드 (1)
      • 🌌 React.js (1)
    • 😎 주저리 (2)

블로그 메뉴

  • 💻Github
  • 📸Instagram
  • ✨Blog

공지사항

인기 글

태그

  • 슬랙알림
  • 깃헙액션
  • 스프링부트
  • springboot
  • SlackAPI
  • github
  • AWS
  • nodejs
  • nestjs
  • OAuth
  • oauth2
  • 도커
  • 네스트
  • JWT
  • docker
  • 스프링
  • GithubActions
  • nest
  • JSON
  • Spring

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
gengminy

갱미니의 코딩일지

📡 백엔드/⭐ Node.js

[Nodejs] express 서버 열기

2022. 5. 15. 23:51

✅ package.json 초기화 및 express 모듈 설치

npm init
...
npm install express

 

✅ express import 및 express 객체 생성

const express = require('express');
const app = express();

 

✅ app.set 과 app.get

  • app.set(키, 값)으로 데이터 저장
  • app.get(키)로 데이터 가져올 수 있음

 

✅ GET 메소드 라우팅

app.get('/', (req, res)=>{
    res.send('<h1>Welcome!</h1>');
});

 

✅ 서버 실행

app.listen(app.get('port'), ()=>{
    console.log(`Server online on port ${app.get('port')}`);
});

 

✅ HTML 응답을 위한 path 모듈

const path = require('path');

 

✅ HTML 파일 응답

const express = require('express');
const path = require('path');

const app = express();

app.set('port', process.env.PORT || 3000);

app.get('/', (req, res)=>{
    res.sendFile(path.join(__dirname, '/src/index.html'));
});

app.listen(app.get('port'), ()=>{
    console.log(`Server online on port ${app.get('port')}`);
});

__dirname 은 app.js 파일의 경로를 리턴해준다

저작자표시 (새창열림)

'📡 백엔드 > ⭐ Node.js' 카테고리의 다른 글

[Nodejs] Sequelize 로 SQL 데이터베이스 연동하기  (0) 2022.05.21
[Nodejs] Router 객체로 라우팅 파일 분리  (0) 2022.05.17
[Nodejs] 미들웨어의 사용과 자주 쓰이는 미들웨어  (0) 2022.05.16
[Nodejs] 쿠키와 세션  (2) 2022.05.15
[Nodejs] http 모듈로 간단한 REST API 사용  (0) 2022.05.15
    '📡 백엔드/⭐ Node.js' 카테고리의 다른 글
    • [Nodejs] Router 객체로 라우팅 파일 분리
    • [Nodejs] 미들웨어의 사용과 자주 쓰이는 미들웨어
    • [Nodejs] 쿠키와 세션
    • [Nodejs] http 모듈로 간단한 REST API 사용
    gengminy
    gengminy
    코딩

    티스토리툴바