Nodejs mvc mysql

Trong các bài trước chúng ta đã tìm hiểu sơ lược về Express, trong phần này chúng ta sẽ sử dụng Express để xây dựng một ứng dụng ghi chú đơn giản, sử dụng mô hình MVC

Mô hình MVC trong Express

Bản thân Express không được xây dựng theo mô hình MVC, nhưng mô-đun

var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
4 được sử dụng để tạo dự án Express thì lại tạo cho chúng ta một ứng dụng gần giống với MVC bởi vì dự án này tồn tại ở thứ 2

  • Thư mục
    var notes = [];
    exports.update = exports.create = function[key, title, body] {
        notes[key] = { title: title, body: body };
    }
    
    exports.read = function[key] {
        return notes[key];
    }
    
    exports.destroy = function[key] {
        delete notes[key];
    }
    
    exports.keys = function[] {
        return Object.keys[notes];
    }
    
    5 chứa các tệp mẫu [tệp có phần mở rộng là
    var notes = [];
    exports.update = exports.create = function[key, title, body] {
        notes[key] = { title: title, body: body };
    }
    
    exports.read = function[key] {
        return notes[key];
    }
    
    exports.destroy = function[key] {
        delete notes[key];
    }
    
    exports.keys = function[] {
        return Object.keys[notes];
    }
    
    6], các tệp này được sử dụng để hiển thị dữ liệu, tức là tương tự với các phần Lượt xem trong MVC
  • Thư mục
    var notes = [];
    exports.update = exports.create = function[key, title, body] {
        notes[key] = { title: title, body: body };
    }
    
    exports.read = function[key] {
        return notes[key];
    }
    
    exports.destroy = function[key] {
        delete notes[key];
    }
    
    exports.keys = function[] {
        return Object.keys[notes];
    }
    
    7 được sử dụng để chuyển hướng các URL đến các hàm xử lý tương ứng, tức là tương tự với Bộ điều khiển trong MVC

Do đó, để ứng dụng của chúng ta vận hành theo đúng mô hình MVC, thành phần còn thiếu là Model. Mô hình có chức năng lưu trữ dữ liệu, thay đổi/cập nhật dữ liệu, hỗ trợ truy vấn dữ liệu. Và dĩ nhiên là mô hình lưu trữ mã cũng nên được lưu trong một thư mục riêng biệt tách rời với

var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
5 và
var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
7

Tạo ứng dụng Ghi chú

Chúng ta sẽ xây dựng ứng dụng Notes [quản lý ghi chú] đơn giản

Đầu tiên chúng ta tạo dự án

C:\NodeJS>express --ejs notes
...

Sau đó cài đặt các module đi kèm

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...

Tạo mô hình

Trong thư mục gốc của dự án, chúng tôi tạo một thư mục có tên

...
var notes = require['./routes/notes'];
...
0 nằm chung với các thư mục
var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
5,
var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
7… Trong thư mục
...
var notes = require['./routes/notes'];
...
0, chúng tôi tạo một tệp có tên
...
var notes = require['./routes/notes'];
...
4 với nội dung sau đây

var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}

Trong đoạn mã trên chúng ta tạo một mảng có tên

...
var notes = require['./routes/notes'];
...
5 dùng để lưu trữ các ghi chú, mỗi ghi chú bao gồm
...
var notes = require['./routes/notes'];
...
6[id],
...
var notes = require['./routes/notes'];
...
7[tiêu đề] và
...
var notes = require['./routes/notes'];
...
8 [nội dung]. Trong phần này chúng tôi chỉ thực hiện lưu trữ trong bộ nhớ RAM để đơn giản, tức là làm mỗi lần tắt/khởi động máy chủ thì các ghi chú sẽ bị xóa, trong các phần sau chúng tôi sẽ sử dụng cơ sở dữ liệu để lưu trữ

Ở đoạn mã trên hàm

...
var notes = require['./routes/notes'];
...
9 và hàm

    
    


    

Trong tệp


3 chúng tôi sử dụng biến

4 chúng tôi sử dụng biến này để lấy
...
var notes = require['./routes/notes'];
...
6 của từng ghi chú, sau đó hiển thị một đường liên kết đến
...
var notes = require['./routes/notes'];
...
6 trong đó, biến
...
var notes = require['./routes/notes'];
...
5 sẽ được gửi đến từ hàm

8, và đó sẽ là bất kỳ biến nào

var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;

Chúng ta thêm dòng


    
    


    

2 sau đó truyền vào lời gọi

:

3 để gửi tệp

2 có thể sử dụng

:

5

Đến đây chúng ta có thể chạy thử ứng dụng

C:\NodeJS\notes>npm start

7 chúng ta thêm đoạn mã sau

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
0

Đoạn mã trên rất đơn giản, chúng ta sử dụng hàm

:

3 để gọi tệp

:

9 và truyền tham số
var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
0 vào tệp này rồi trả về cho người dùng. Ở đây tham số
var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
1 cho biết chúng ta đang cập nhật hay tạo mới một ghi chú, nếu là cập nhật thì tham số
var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
2 sẽ là một đối tượng nào đó, còn tạo mới thì đối tượng này sẽ trống
var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
3 trong phần cập nhật

Tiếp theo trong thư mục

var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
4 chúng ta tạo tệp

:

9 với nội dung như sau

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
1

Như chúng ta đã biết, cả phần tạo và cập nhật ghi chú sử dụng chung một hàm, và sử dụng chung một mẫu là tệp

var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
6 Ở đây chúng ta tạo một biểu mẫu có các trường để nhập dữ liệu cho một ghi chú, nếu thao tác là

Biểu mẫu này sẽ gửi đến trang

var express = require['express'];
var router = express.Router[];
var notes = require['../models/notes'];

/* GET home page. */

router.get['/', function[req, res, next] {
    res.render['index', { title: 'Notes', notes: notes }];
}];

module.exports = router;
7 và biểu mẫu này sử dụng phương thức POST để gửi, do đó bây giờ chúng ta phải làm thêm trang này. Trong file

    
    


    

3 chúng ta cũng kiểm tra dữ liệu để xem
...
app.use['/noteadd', notes.add];
...
5 . Bởi vì người dùng có thể nhập đường dẫn bằng tay chứ không chỉ có sử dụng chuột để nhấp vào đường liên kết trên trang web

Cuối cùng chúng ta tạo một tệp có tên

...
app.use['/noteadd', notes.add];
...
6 trong thư mục
var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
5 với nội dung như sau

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
6

Đoạn mã trên sẽ chịu trách nhiệm hiển thị nội dung ghi chú. Ngoài ra còn hiển thị 2 đường liên kết đến trang

...
app.use['/noteadd', notes.add];
...
8 dùng để xóa ghi chú và
...
app.use['/noteadd', notes.add];
...
9 dùng để chỉnh sửa ghi chú

7 chúng ta thêm đoạn mã sau

var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
0

Cũng tương tự như các chức năng khác, ở đây chúng ta kiểm tra xem dữ liệu gửi lên có hợp lệ hay không rồi trả về trong trang

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
12

Kế tiếp chúng ta tạo file

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
13 trong thư mục
var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
5 như sau

var notes = [];
exports.update = exports.create = function[key, title, body] {
    notes[key] = { title: title, body: body };
}

exports.read = function[key] {
    return notes[key];
}

exports.destroy = function[key] {
    delete notes[key];
}

exports.keys = function[] {
    return Object.keys[notes];
}
1

Ở đây chúng tôi hiển thị một biểu mẫu cho người dùng xác nhận việc xóa tệp, biểu mẫu này sẽ gửi đến đường dẫn

C:\NodeJS>cd notes
C:\NodeJS\notes>npm install
...
15 với phương thức POST. Do đó bây giờ chúng ta phải xử lý đường dẫn này

Chủ Đề