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
Was this helpful?