You would use the UPDATE method.
The DELETE and the UPDATE method requires an ID parameter. /thing/:id
From what I can tell, the REST API portion of the application is the part that controls where or what data is being manipulated. CRUD is when you define a function or method to happen to the data.
So REST are the nouns/resources.
CRUD are the verbs/actions to the resources.
Create an express app, (npx create-express-api server) and install packages, dependencies, etc. (mongo, nodemon, cors, dotenv)
Define a schema for that resource, created in a separate file this will be used for front and back end requests and responses and how we receive the response or send out request.
// this will be in a seperate folder, usually a models folder in a file called ThingModel.js
const mongoose = require('mongoose');
const thingSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
isCool: {
type: Boolean,
required: true
}
});
module.exports = mongoose.model('Thing', thingSchema)
// This will be in routes or modules folder inside of a things.js
const express = require('express');
const Thing = require('../models/ThingModel')
const router = express.Router();
router.get('/things', async (request, response) => {
try {
const thing = await Thing.find();
response.status(200).json(thing);
} catch (error) {
response.status(500).json('server side error')
}
})
module.exports = router;
// this will be in our server.js file
require('dotenv').config()
const express = require('express');
const cors = require('cors');
const app = express();
const Things = require('./routes/things');
const PORT = process.env.PORT
const MONGO_DB_URI = process.env.MONGO_DB_URI;
app.use(cors());
app.use(express.json());
app.use('/', Things)
mongoose.connect(DATABASE_URL)
.then(() => {
app.listen(PORT, () => console.log(`Connected to mongodb and listening on ${PORT}`));
})
.catch((error) => {
console.log(error);
});
NODE_ENV=development
PORT=3001
MONGO_DB_URI=mongodb+srv://lskjdlfjjwieff//somecrazyurlstufff
What makes a RESTful API stateless and how to keep them indepenedent from each other
Best practices of middleware coding, such as app.get()
References
SPEED RUN: Build a CRUD API with Node.js + Express + MongoDB