http와 express

데브코스

목록 보기
21/131

요즘에는 node.js로 express를 붙여서 백엔드를 만들고 있다고 함.

http는 내장모듈

express는 외장 모듈


이건 http

let http = require('http');

function onRequest(request, response) {
    response.writeHead(200, {'Content-Type' : 'tex/html'});
    response.write('Hello Node.js');
    response.end();
}

http.createServer(onRequest).listen(8888);

이게 express

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

app.get("/", function (req, res) {
  res.send("Hello world");
});

app.listen(3000);

메소드는 GET이고 URL은 /이거

여러 API만드는 법!!

만약 //API : GET + "http://localhost:1234/test"

이렇게 만들어주고 싶다 하면!

app.get("/test", function (req, res) {
  res.send("Test");
}); 

이걸 적어주면 돼!

**근데 이게 어떻게 test라는 url로 들어가면 Test라는 말을 남겨주는거지?? 할 수 있는데,

get 메소드로 /test 같은 주소가 날라오면, 매개변수로 전달받은 콜백 함수를 호출하겠어!! => 서버에 셋팅해둠**
라는 뜻이야

profile
Dive Head First | Work Super Hard | Attract Great People

0개의 댓글