npm install express
Express는 웹 애플리케이션을 위한 일련의 기능들을 제공하는 Node.js 라이브러리로, 다양한 HTTP 메소드를 제공한다.
HTTP
let http = require("http");
function onRequest(request, response) {
response.writeHead(200, { "Content-Type": "text/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);
Express를 사용하면 코드가 더 짧아지고, 가독성이 좋아진다.
Get 방식으로 http://localhost:3000/test
페이지 만들기
app.get("/test", function (req, res) {
res.send("TEST");
});
Get 방식으로 http://localhost:3000/test/1
페이지 만들기
app.get("/test/1", function (req, res) {
res.send("One!!");
});
자바스크립트의 객체는 키(key)과 값(value)으로 구성된 프로퍼티(Property)들의 집합이다.
//객체 생성
let nodejsBook = {
title : 'Node.js를 공부해보자.',
price : 20000,
description : "이 책의 내용은 ...."
}
// 객체의 값에 접근
function print(book){
console.log(book.title);
console.log(book.price);
console.log(book.description);
}
print(nodejsBook);
오늘의 소감
오늘은 Express를 설치하고 처음으로 사용해봤다.
슬슬 본격적으로 백엔드의 공부를 진행하고, 구현해볼 생각에 설레기도 하고, 학교의 마지막 기말고사도 앞두고 있어 바쁠것 같아 약간의 스트레스도 있다. 그래도 다 잘 마무리하고, 열심히 공부하면 좋은 결과가 있을거라 생각이 든다.