Express Framework

이연희·2022년 1월 1일
0

Node.js

목록 보기
3/4

express
Node.js를 기반으로 REST 웹서버를 구축하기 위한 프레임워크

✅ expree 패키지 설치후 package.json의 dependency
💡 package.json을 이용하여 소스코드를 이동시킬 때마다 모듈, 버전을 설치하여 다른 환경에서도 프로그램 실행 가능

{
  "name": "medium-nodejs-express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/serendipity1004/medium-nodejs-express.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/serendipity1004/medium-nodejs-express/issues"
  },
  "homepage": "https://github.com/serendipity1004/medium-nodejs-express#readme",
  "dependencies": {
    "express": "^4.16.4"
  }
}

✅ express 연동과 3000번 포트로 서버 구동
const express = require('express');
const app = express();
const server = app.listen(3000,()=>{
  console.log('Start Server:localhost:3000');
});

✅ 라우터 생성

💡 라우팅: 어떤 버튼을 누르면 리퀘스트가 가는데, 그 리퀘스트를 서버의 어떤 기능과 맵핑할지 결정하는 것을 라우팅이라고 한다.

  • localhost:3000 -> hello world
  • localhost:3000/about -> about page
app.get('/',function (req,res){
  res.send('hello world')
})
app.get('/about',function (req,res){
  res.send('about page')
})

[Github : web_server_program.js](https://github.com/yhLee11/nodejs/blob/master/web_server_program.js)
profile
공부기록

0개의 댓글