Ref: https://www.youtube.com/watch?app=desktop&v=YGxrvHGCJ2Y
Frontend to Backend: Application layer(應用層) under TCP/IP model
List of Protocols in Application layer: https://www.geeksforgeeks.org/application-layer-protocols-in-tcp-ip/
GET Method usually does not include message body.
Message bodies are appropriate for some request methods and inappropriate for others. For example, a request with the POST method, which sends input data to the server, has a message body containing the data. A request with the GET method, which asks the server to send a resource, does not have a message body.
A database, in the most general sense, is an organized collection of data. More specifically, a database is an electronic system that allows data to be easily accessed, manipulated and updated.
When people refer to a “database” they’re often talking about a computer program that allows them to interact with their database. These programs, known more formally as a database management system (DBMS), are often installed on a virtual private server and accessed remotely.
Problems with RDBMS:
When interacting with a database using OOP languages, you'll have to perform different operations like creating, reading, updating, and deleting (CRUD) data from a database. By design, you use SQL for performing these operations in relational databases.
While using SQL for this purpose isn't necessarily a bad idea, the ORM and ORM tools help simplify the interaction between relational databases and different OOP languages.
For example, if we want to find a todo item named "buying an apple" and haven't done(done = False) in Todo
database, we can access it by using ORM syntax:
Todo.find ({ name: 'buying an apple', done: { false }})
Using SQL syntax:
SELECT * FROM Todos WHERE name='buying an apple' AND done=FALSE;
Document-Oriented: Mixture of Relational & Key-Value Databases
Todo.find()[0].username
{
"_id": { "$oid": "6572e1cef219bca05511d230" },
"id": { "$numberInt": "1" },
"username": "admin",
"passwd": "$2a$10$/sbmKQjOpqfeeZ1gFEyaauNC5GxNL6yTgJP/AIbGCN9DpyxOMv9Ou",
"identity": "Admin",
"events": [],
"isLoggedIn": false,
"loggedInAt": { "$date": { "$numberLong": "1672993352831" } },
"__v": { "$numberInt": "0" }
}
https://lyunotes.blogspot.com/2020/11/day12-mongodb-atlas.html
https://www.mongodb.com/
https://www.mongodb.com/atlas/database
In server.js
:
const mongoose = require("mongoose");
require("dotenv").config();
const { MONGO_USERNAME, MONGO_PASSWORD } = process.env;
const uri = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@cluster0.ogm4i.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
// this can be copied in MongoDB Atlas
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("err", (err) => {
console.error(err);
});
db.on("open", () => {
console.log("Successfully connect to mongoDB");
});
in .env
:
MONGO_USERNAME=
MONGO_PASSWORD=
Install dotenv-defaults
if Error
npm install dotenv
Create a model
directory
In user.js
:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
id: {
type: Number,
required: [true, "Id field is required."],
},
name: {
type: String,
required: [true, "Name field is required."],
},
});
// Creating a table within database with the defined schema
const User = mongoose.model("User", UserSchema);
// Exporting table for querying and mutating
module.exports = User;
In server.js
: Import User Schema
const User = require("./model/user");
In server.js
:
const saveUser = async (id, name) => {
const existing = await User.findOne({ name: name });
if (existing) {
console.log(`Username ${name} already exists!!`);
return;
}
try {
const newUser = new User({ id, name });
console.log("Created user", newUser);
await newUser.save();
console.log("Successfully saved");
return;
} catch (e) {
throw new Error("User creation error: " + e);
}
};
const DeleteDB = async () => {
try {
await User.deleteMany({});
console.log("Database deleted");
} catch (e) {
throw new Error("Database deletion failed");
}
};
In server.js
const router = require("router");
router.post("/saveuser", saveUser)
// More Routers
app.use("/", router)
Or just
app.route("/saveuser")
.get((req, res) => {
// do something
})