728x90
반응형
저번에 npm 으로 이것저것 세팅할 때 entry point 를 server.js 로 설정했었다
그러니 server.js 라는 파일을 만들어준다.
여기에 코딩 시작하면됨.
GET 요청 처리하기
const express = require('express');
const app = express();
app.listen();
위는 서버를 띄우기 위한 기본 셋팅 (express 라이브러리)
listen(서버 띄울 포트 번호, 띄운 후 실행할 코드)
const express = require("express");
const app = express();
app.listen(8080, function () {
console.log("listening on 8080");
});
서버에 접속해보자
GET 요청을 처리하는 코드를 작성해보자
const express = require("express");
const app = express();
app.listen(8080, function () {
console.log("listening on 8080");
});
app.get("/pet", function (요청, 응답) {
응답.send("펫용품 쇼핑 페이지");
});
nodemon 설치하기
코드 수정하면 바로 반영되는게 아니라
서버를 재실행 해야하는데
서버 껐다가 켜는 작업을 자동화 시켜보자
npm 으로 nodemon 이라는 걸 깔면 된다.
npm install -g nodemon
서버에 HTML 파일 전송해보기
이번에는 특정 주소로 접속했을 때 HTML 파일을 보내도록 해보자.
(/ 하나만 쓰면 홈임)
const express = require("express");
const app = express();
app.listen(8080, function () {
console.log("listening on 8080");
});
app.get("/", function (요청, 응답) {
응답.sendFile(__dirname + "/index.html");
});
이번엔 send 가 아니라 sendFile 을 써서 html 파일 경로를 적어주었다.
index.html 은 따로 파일을 생성해주었다
728x90
반응형