Express Basics

image-20200331092006288

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

  1. Allows to set up middlewares to respond to HTTP Requests

  2. Defines a routing table which is used to perform different actions based on HTTP Method and URL

  3. Allows to dynamically render HTML Pages based on passing arguments to templates

Installation

1. Installing Express Locally

Install express

npm install express --save
  • saves the installation locally in the node_modules

  • create a directory express inside node_modules

Express version check

$ npm view express version
4.17.1

Install modules alog with express

npm install body-parser --save
npm install cookie-parser --save
npm install multer --save

2. Installing Express in the Project

Directory structure

express_tutorial/
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ public
โ”‚   โ””โ”€โ”€ css
โ”‚   โ””โ”€โ”€ style.css
โ”œโ”€โ”€ router
โ”‚   โ””โ”€โ”€ main.js
โ”œโ”€โ”€ server.js
โ””โ”€โ”€ views
 โ”œโ”€โ”€ about.html
 โ””โ”€โ”€ index.html

1. create package.json

{
  "name": "express-tutorial",
  "version": "1.0.0",
  "dependencies": 
  {
    "express": "~4.13.1",
    "ejs": "~2.4.1"    
  }
}

2. Install dependency by using npm

npm install

3. Create Express server

server.js

var express = require('express');
var app = express()
var server = app.listen(8000, function(){
    console.log("Express server running on port 8000!")
})

result

$ node server.js
Express server running on port 8000!
  • port 8000 ์œผ๋กœ web server ์—ด์–ด์„œ ๋“ค์–ด๊ฐ€๋ฉด Cannot GET/ ์ด๋ผ๋Š” text ์ถœ๋ ฅ๋จ

    • why?

      • Router๋ฅผ ์•„์ง ์ •๋ฆฌํ•˜์ง€ ์•Š์•„์„œ!

4. Basic Routing

server.js

var express = require('express');
var app = express()
var server = app.listen(8000, function(){
    console.log("Express server running on port 8000!")
})

app.get('/', function(request, response){
    response.send('Hello World')
})

5. Router

image-20200403223235009

router code์™€ server code๋Š” ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ์ฝ”๋”ฉ ์Šต๊ด€!

  • router folder ๋งŒ๋“ค๊ธฐ

  • main.js ์ƒ์„ฑ

router > main.js

module.exports = function(app){

    app.get('/', function(request, response){
        response.render('index.html')
    });

    app.get('/about', function(request, response){
        response.render('about.html')
    });
}
  • module.exports๋Š” router code๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— server.js์—์„œ module๋กœ์„œ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ~!

5. Render HTML page

  • views directory ๋งŒ๋“ค๊ธฐ

  • html file๋“ค ๋งŒ๋“ค๊ธฐ

views > index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index</title>
</head>
<body>

<p>This is index page!</p>
</body>
</html>

views > about.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=<device-width>, initial-scale=1.0">
    <title>About</title>
</head>
<body>
    <p>About... what?</p>
</body>
</html>

server.js ์ˆ˜์ •ํ•˜๊ธฐ

var express = require('express');
var app = express();

// router module์ธ main.js๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ app์— ์ „๋‹ฌํ•˜๊ธ”
var router = require('./router/main')(app);

// server๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก HTML์˜ ์œ„์น˜๋ฅผ ์ •์˜ํ•ด์ฃผ๊ธ”
app.set('views',__dirname + '/views');

// server๊ฐ€ HTML rendering์„ ํ•  ๋•Œ EJS engine ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๊ธ”
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

var server = app.listen(8000, function(){
    console.log("Express server has started on port 8000!!")
})

Handling Static Files

Static Files

  • HTML์—์„œ ์‚ฌ์šฉ๋˜๋Š” .js, css, image file๋“ค

  • Server์—์„œ Static files์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด์„  express.static() method๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค!

  1. public directory ๋งŒ๋“ค๊ธฐ

  2. css directory ๋งŒ๋“ค๊ธฐ

  3. .css file๋“ค ๋งŒ๋“ค๊ธฐ

public > css > style.css

body {
    background-color: black;
    color: white;
}

server.js ์ˆ˜์ •ํ•˜๊ธฐ

// public directory๋ฅผ static folder๋กœ ์„ค์ •
app.use(express.static('public'));

html file๋“ค์— css ๋งํฌ ์ถ”๊ฐ€ํ•˜๊ธฐ

<link rel="stylesheet" href="css/style.css">

result

127.0.0.1:8000/

image-20200404155824643

127.0.0.1:8000/about

image-20200404155934497

Last updated

Was this helpful?