Hướng dẫn cách học nodejs

Hướng dẫn cách học nodejs

Nguyen Van Giang @Giangnv

Theo dõi

1.9K 137 47

Đã đăng vào thg 1 24, 2019 4:14 SA 8 phút đọc

6.3K

0

5

Tự học nodejs từ đầu

  • Báo cáo
  • Thêm vào series của tôi

Bài đăng này đã không được cập nhật trong 3 năm

1.Cài đặt

  • Kiểm tra xem đã cài đặt node và npm hay chưa bằng câu lệnh sau:
     giang@ubuntu  ~/projects  npm -v
     5.6.0
     giang@ubuntu  ~/projects  node -v
     v9.11.2
     giang@ubuntu  ~/projects 
    
  • Nếu bạn chưa cài hoặc phiên bản đã cũ thì ta sẽ lên trang chủ https://nodejs.org/en/ để check phiên bản mới nhất và cài đặt.
  • Cài đặt nodejs:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
  • File setup_10.x tương đương với phiên bản 10.15.0 hiện tại đang được Recommended for most users hiện tại
  • Và tương tự cài đặt npm:
    sudo apt install npm
    

2.Tạo server nodejs

  • Để tạo server nodejs ta có thể sử dụng module
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    0
  • Trước tiên ta cần tạo file javascript với tên app.js và nội dung như sau:
    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  • Và chạy câu lệnh sau để khởi tạo server:
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
  • Kiểm tra kết quả trên trình duyệt với dường dẫn http://localhost:3000/ ta sẽ thu được kết quả
    Hello World
    

3.Cài đặt Express

  • Express là một web application frameword Nodejs nhỏ và linh hoạt, nó cung cấp một loạt các tính năng cho web và ứng dụng di động.
  • Ngoài ra Express còn có vô số phương thức tiện ích HTTP và middleware bạn muốn, giúp thao tác với API nhanh và dễ dàng hơn.
  • Trước khi cài Express ta cần tạo file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    1 trong project bằng câu lệnh khởi tạo
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    2
    giang@ubuntu  ~/projects  mkdir learn_nodejs
    giang@ubuntu  ~/projects  cd learn_nodejs 
    giang@ubuntu  ~/projects/learn_nodejs  npm init
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help json` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (learn_nodejs) learn_nodejs
    version: (1.0.0) 
    description: 
    entry point: (index.js) 
    test command: 
    git repository: 
    keywords: 
    author: 
    license: (ISC) 
    About to write to /home/giang/projects/lean_nodejs/learn_nodejs/package.json:
    
    {
      "name": "learn_nodejs",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
    
    
    Is this ok? (yes) 
    giang@ubuntu  ~/projects/lean_nodejs/learn_nodejs  ls
    package.json
    
  • Trong lúc khởi tạo, nó sẽ hỏi bạn 1 vài thông tin
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    3,
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    4,.. nếu không thay đổi gì thì bạn chỉ việc enter
  • Tiếp đến là cài đặt express:
    npm install express --save
    
  • Sau khi cài đặt xong kết quả thu được như sau:
    giang@ubuntu  ~/projects/learn_nodejs  ls
    app.js  node_modules  package.json  package-lock.json
    

4.Basic Express Routing

  • Express giúp tạo route trong nodejs một cách dễ dàng. Ví dụ ta sẽ tạo 2 route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    5 và
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    6
  • Route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    5 hiển thị text: "Welcome to Express"
  • Route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    6 hiển thị text: "This is about page"
  • Ta mở file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    9 và update lại như sau:
    const express = require('express');
    const app = express()
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    app.get('/home', function (req, res) {
      res.send('Welcome to Express');
    });
    
    app.get('/about', function (req, res) {
      res.send('This is about page')
    });
    
    app.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  • Khởi tạo lại server với câu lệnh
    Hello World
    
    0 ta sẽ thu được kết quả ở 2 url http://localhost:3000/home và http://localhost:3000/about

5.File tĩnh

  • Express cho phép ta load file tĩnh bằng cách sử dụng
    Hello World
    
    1, chẳng hạn ở đây ta có thể load file image, css, javascript,..
  • Ta sẽ làm ví dụ load file bootstrap ở đây, nếu chưa có file bootstrap thì ta có thể lên trang https://getbootstrap.com/ tải về.
  • Tạo thư mục public với 2 thư mục con css và js như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    0
  • Tiếp đến ta sẽ cài đặt , express hỗ trợ rất nhiều template engine nhưng ở đây ta sẽ sử dụng template engine handlebars.js (hbs)
  • Cài đặt
    Hello World
    
    2:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    1
  • Sau đó ta sẽ tạo file index.hbs bên trong thư mục views mới với nội dung:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    2
  • Và update file app.js
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    3
  • Chạy thử ta sẽ thu được kết quả như sau:
    Hướng dẫn cách học nodejs

6.Truyền data từ server nodejs đến view

  • Update file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    9 như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    4
  • Và file index.hbs như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    5
  • Kết quả ta thu được với url http://localhost:3000/Giang
    Hướng dẫn cách học nodejs
  • Còn mặc định nếu ta test với url http://localhost:3000 thì kết quả sẽ là "Hello, M Fikri"

7.Tạo form post data đến view

  • Để post được data input đến view ta cần cài đặt thêm
    Hello World
    
    4 bằng câu lệnh sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    6
  • Tạo form.hbs trong thư mục views:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    7
  • File form.hbs có nội dung như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    8
  • File app.js sẽ được update 2 route thêm như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    9
  • Kiểm tra kết quả ta sẽ thu được như sau:
    Hướng dẫn cách học nodejs
  • Và subit sẽ thu được
    Hướng dẫn cách học nodejs

Tài liệu tham khảo

nodejs.org

http://mfikri.com/en/blog/nodejs-tutorial

Express

Node.js


All rights reserved

  • Báo cáo
  • Thêm vào series của tôi

Hướng dẫn cách học nodejs

Nguyen Van Giang @Giangnv

Theo dõi

1.9K 137 47

Đã đăng vào thg 1 24, 2019 4:14 SA 8 phút đọc

6.3K

0

5

Tự học nodejs từ đầu

  • Báo cáo
  • Thêm vào series của tôi

Bài đăng này đã không được cập nhật trong 3 năm

1.Cài đặt

  • Kiểm tra xem đã cài đặt node và npm hay chưa bằng câu lệnh sau:
     giang@ubuntu  ~/projects  npm -v
     5.6.0
     giang@ubuntu  ~/projects  node -v
     v9.11.2
     giang@ubuntu  ~/projects 
    
  • Nếu bạn chưa cài hoặc phiên bản đã cũ thì ta sẽ lên trang chủ https://nodejs.org/en/ để check phiên bản mới nhất và cài đặt.
  • Cài đặt nodejs:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
  • File setup_10.x tương đương với phiên bản 10.15.0 hiện tại đang được Recommended for most users hiện tại
  • Và tương tự cài đặt npm:
    sudo apt install npm
    

2.Tạo server nodejs

  • Để tạo server nodejs ta có thể sử dụng module
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    0
  • Trước tiên ta cần tạo file javascript với tên app.js và nội dung như sau:
    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  • Và chạy câu lệnh sau để khởi tạo server:
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
  • Kiểm tra kết quả trên trình duyệt với dường dẫn http://localhost:3000/ ta sẽ thu được kết quả
    Hello World
    

3.Cài đặt Express

  • Express là một web application frameword Nodejs nhỏ và linh hoạt, nó cung cấp một loạt các tính năng cho web và ứng dụng di động.
  • Ngoài ra Express còn có vô số phương thức tiện ích HTTP và middleware bạn muốn, giúp thao tác với API nhanh và dễ dàng hơn.
  • Trước khi cài Express ta cần tạo file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    1 trong project bằng câu lệnh khởi tạo
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    2
    giang@ubuntu  ~/projects  mkdir learn_nodejs
    giang@ubuntu  ~/projects  cd learn_nodejs 
    giang@ubuntu  ~/projects/learn_nodejs  npm init
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help json` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (learn_nodejs) learn_nodejs
    version: (1.0.0) 
    description: 
    entry point: (index.js) 
    test command: 
    git repository: 
    keywords: 
    author: 
    license: (ISC) 
    About to write to /home/giang/projects/lean_nodejs/learn_nodejs/package.json:
    
    {
      "name": "learn_nodejs",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
    
    
    Is this ok? (yes) 
    giang@ubuntu  ~/projects/lean_nodejs/learn_nodejs  ls
    package.json
    
  • Trong lúc khởi tạo, nó sẽ hỏi bạn 1 vài thông tin
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    3,
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    4,.. nếu không thay đổi gì thì bạn chỉ việc enter
  • Tiếp đến là cài đặt express:
    npm install express --save
    
  • Sau khi cài đặt xong kết quả thu được như sau:
    giang@ubuntu  ~/projects/learn_nodejs  ls
    app.js  node_modules  package.json  package-lock.json
    

4.Basic Express Routing

  • Express giúp tạo route trong nodejs một cách dễ dàng. Ví dụ ta sẽ tạo 2 route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    5 và
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    6
  • Route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    5 hiển thị text: "Welcome to Express"
  • Route
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    6 hiển thị text: "This is about page"
  • Ta mở file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    9 và update lại như sau:
    const express = require('express');
    const app = express()
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    app.get('/home', function (req, res) {
      res.send('Welcome to Express');
    });
    
    app.get('/about', function (req, res) {
      res.send('This is about page')
    });
    
    app.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    
  • Khởi tạo lại server với câu lệnh
    Hello World
    
    0 ta sẽ thu được kết quả ở 2 url http://localhost:3000/home và http://localhost:3000/about

5.File tĩnh

  • Express cho phép ta load file tĩnh bằng cách sử dụng
    Hello World
    
    1, chẳng hạn ở đây ta có thể load file image, css, javascript,..
  • Ta sẽ làm ví dụ load file bootstrap ở đây, nếu chưa có file bootstrap thì ta có thể lên trang https://getbootstrap.com/ tải về.
  • Tạo thư mục public với 2 thư mục con css và js như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    0
  • Tiếp đến ta sẽ cài đặt , express hỗ trợ rất nhiều template engine nhưng ở đây ta sẽ sử dụng template engine handlebars.js (hbs)
  • Cài đặt
    Hello World
    
    2:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    1
  • Sau đó ta sẽ tạo file index.hbs bên trong thư mục views mới với nội dung:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    2
  • Và update file app.js
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    3
  • Chạy thử ta sẽ thu được kết quả như sau:
    Hướng dẫn cách học nodejs

6.Truyền data từ server nodejs đến view

  • Update file
     giang@ubuntu  ~/projects  node app.js        
     Server running at http://127.0.0.1:3000/
    
    9 như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    4
  • Và file index.hbs như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    5
  • Kết quả ta thu được với url http://localhost:3000/Giang
    Hướng dẫn cách học nodejs
  • Còn mặc định nếu ta test với url http://localhost:3000 thì kết quả sẽ là "Hello, M Fikri"

7.Tạo form post data đến view

  • Để post được data input đến view ta cần cài đặt thêm
    Hello World
    
    4 bằng câu lệnh sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    6
  • Tạo form.hbs trong thư mục views:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    7
  • File form.hbs có nội dung như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    8
  • File app.js sẽ được update 2 route thêm như sau:
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
    9
  • Kiểm tra kết quả ta sẽ thu được như sau:
    Hướng dẫn cách học nodejs
  • Và subit sẽ thu được
    Hướng dẫn cách học nodejs

Tài liệu tham khảo

nodejs.org

http://mfikri.com/en/blog/nodejs-tutorial

Express

Node.js


All rights reserved

  • Báo cáo
  • Thêm vào series của tôi