Express Basics
What is Express.js?
: Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web
and mobile
applications.
Core Features
Allows to set up middlewares to respond to HTTP Requests
Defines a routing table which is used to perform different actions based on HTTP Method and URL
Allows to dynamically render HTML Pages based on passing arguments to templates
Installation
1. Installing Express Locally
Install express
saves the installation locally in the node_modules
create a directory express inside node_modules
Express version check
Install modules alog with express
2. Installing Express in the Project
Directory structure
1. create package.json
2. Install dependency by using npm
npm
3. Create Express
server
Express
serverserver.js
result
port 8000 으로 web server 열어서 들어가면
Cannot GET/
이라는 text 출력됨why?
Router
를 아직 정리하지 않아서!
4. Basic Routing
server.js
5. Router
router code와 server code는 다른 파일에 작성하는 것이 좋은 코딩 습관!
router folder 만들기
main.js
생성
router > main.js
module.exports
는 router code를 따로 작성했기 때문에server.js
에서 module로서 불러와서 사용 가능~!
5. Render HTML page
views directory 만들기
html
file들 만들기
views > index.html
views > about.html
server.js 수정하기
Handling Static Files
Static Files
HTML에서 사용되는
.js
,css
,image
file들Server에서
Static files
을 다루기 위해선express.static()
method를 사용하면 된다!
public directory 만들기
css directory 만들기
.css
file들 만들기
public > css > style.css
server.js 수정하기
html file들에 css 링크 추가하기
result
127.0.0.1:8000/
127.0.0.1:8000/about
Last updated