- MEAN.JS 教程
- MEAN.JS - 首頁
- MEAN.JS - 概述
- MEAN.JS - 架構
- 構建 Node Web 應用
- MEAN.JS - Mean 專案設定
- 構建靜態路由 Node Express
- MEAN.JS - 資料模型構建
- MEAN.JS - REST API
- 使用 Angular 的前端
- 應用中的 Angular 元件
- 使用 Angular 構建單頁應用
- 構建單頁應用:進階
- MEAN.JS 有用資源
- MEAN.JS - 快速指南
- MEAN.JS - 有用資源
- MEAN.JS - 討論
MEAN.JS - 資料模型構建
本章將演示如何在我們的 Node-express 應用中使用資料模型。
MongoDB 是一個開源的 NoSQL 資料庫,它以 JSON 格式儲存資料。它使用面向文件的資料模型來儲存資料,而不是像關係型資料庫那樣使用表和行。本章將使用 Mongodb 來構建資料模型。
資料模型指定文件中存在哪些資料,以及文件中應該有哪些資料。請參考MongoDB 官方安裝文件安裝 MongoDB。
我們將使用上一章的程式碼。您可以從此連結下載原始碼。下載 zip 檔案;將其解壓到您的系統中。開啟終端並執行以下命令來安裝 npm 模組依賴項。
$ cd mean-demo $ npm install
嚮應用程式新增 Mongoose
Mongoose 是一個數據建模庫,它透過增強 MongoDB 的功能來指定資料的環境和結構。您可以透過命令列將 Mongoose 安裝為 npm 模組。轉到您的根資料夾並執行以下命令:
$ npm install --save mongoose
以上命令將下載新包並將其安裝到node_modules資料夾中。--save標誌會將此包新增到package.json檔案中。
{
"name": "mean_tutorial",
"version": "1.0.0",
"description": "this is basic tutorial example for MEAN stack",
"main": "server.js",
"scripts": {
"test": "test"
},
"keywords": [
"MEAN",
"Mongo",
"Express",
"Angular",
"Nodejs"
],
"author": "Manisha",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.5.13"
}
}
設定連線檔案
為了使用資料模型,我們將使用app/models資料夾。讓我們建立如下所示的students.js模型:
var mongoose = require('mongoose');
// define our students model
// module.exports allows us to pass this to other files when it is called
module.exports = mongoose.model('Student', {
name : {type : String, default: ''}
});
您可以透過建立檔案並在應用程式中使用它來設定連線檔案。在config/db.js中建立一個名為db.js的檔案。檔案內容如下:
module.exports = {
url : 'mongodb://:27017/test'
}
這裡test是資料庫名稱。
這裡假設您已在本地安裝了 MongoDB。安裝完成後,啟動 Mongo 並建立一個名為 test 的資料庫。這個資料庫將有一個名為 students 的集合。向這個集合中插入一些資料。在本例中,我們使用 `db.students.insertOne( { name: 'Manisha' , place: 'Pune', country: 'India'} );` 插入了一條記錄。
將db.js檔案引入應用程式,即在server.js中。檔案內容如下所示:
// modules =================================================
const express = require('express');
const app = express();
var mongoose = require('mongoose');
// set our port
const port = 3000;
// configuration ===========================================
// config files
var db = require('./config/db');
console.log("connecting--",db);
mongoose.connect(db.url); //Mongoose connection created
// frontend routes =========================================================
app.get('/', (req, res) ⇒ res.send('Welcome to Tutorialspoint!'));
//defining route
app.get('/tproute', function (req, res) {
res.send('This is routing for the application developed using Node and Express...');
});
// sample api route
// grab the student model we just created
var Student = require('./app/models/student');
app.get('/api/students', function(req, res) {
// use mongoose to get all students in the database
Student.find(function(err, students) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(students); // return all students in JSON format
});
});
// startup our app at https://:3000
app.listen(port, () ⇒ console.log(`Example app listening on port ${port}!`));
接下來,使用以下命令執行應用程式:
$ npm start
您將看到如下所示的確認資訊:
現在,轉到瀏覽器並輸入https://:3000/api/students。您將看到如下所示的頁面: