20
loading...
This website collects cookies to deliver better user experience
$ npm init -y
$ npm install express cors mongoose
npm install -g nodemon
nodemon server
and this will mean that any changes we make will be taken into account as soon as they are saved. using install -g
means that it is installed globally on our system. const express = require('express');
const app = express();
const cors = require('cors');
const PORT = 4000;
app.use(cors());
app.listen(PORT, function() {
console.log("Server is running on Port: " + PORT);
});
nodemon server
should now show the console log "Server is running on Port: 4000", and we know our server is up and running!const dbURI =
"**insert your URI from MongoDB here, remembering to replace the password and database info for your specific database**";
mongoose
.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
app.listen(PORT);
console.log("Server is running on Port: " + PORT);
})
.catch((err) => {
console.log(err);
});
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
let Lesson = new Schema({
lesson_name: {
type: String,
},
lesson_date: {
type: String,
},
lesson_time: {
type: String,
},
lesson_details: {
type: String,
},
});
module.exports = mongoose.model("Lesson", Lesson);
app.use("/lessons", lessonRoutes)
res.json()
or similar appears somewhere in each request. . This ensures our results are returned in json format. lessonRoutes.route('/').get(function(req, res) {
Lesson.find(function(err, lessons) {
if (err) {
console.log(err);
} else {
res.json(lessons);
}
});
});
lessonRoutes.route('/:id').get(function(req, res) {
let id = req.params.id;
Lesson.findById(id, function(err, lesson) {
res.json(lesson);
});
});
lessonRoutes.route("/add").post(function (req, res) {
let lesson = new Lesson(req.body);
lesson
.save()
.then((lesson) => {
res.status(200).json({ lesson: "lesson added successfully" });
})
.catch((err) => {
res.status(400).send("adding new lesson failed");
});
});
lessonRoutes.route("/update/:id").post(function (req, res) {
Lesson.findByIdAndUpdate(
{ _id: req.params.id },
{
lesson_name: req.body.lesson_name,
lesson_date: req.body.lesson_date,
lesson_time: req.body.lesson_time,
lesson_details: req.body.lesson_details,
},
function (err, result) {
if (err) {
res.send(err);
} else {
res.send(result);
}
}
);
});
npm install axios
import axios from "axios;
axios.get()
. The request takes in one parameter, - the URI for the corresponding endpoint. axios
.get("http://localhost:4000/lessons/" + id)
.then((response) => {
const lessonData = response.data;
updateSpecificLesson(lessonData);
})
.catch(function (error) {
console.log(error);
});
axios
.get("http://localhost:4000/lessons/")
.then((response) => {
const allData = response.data;
updateLessonList(allData);
})
.catch(function (error) {
console.log(error);
});
axios.post()
. The request takes in two parameters - the URI for the corresponding endpoint, and the data to be passed to the database (object). axios.post("http://localhost:4000/lessons/add", newLesson).then(
(response) => {
console.log(response);
updateFormData(initialFormData);
window.location = "/";
},
(error) => {
console.log(error);
}
);
};
axios.post("http://localhost:4000/lessons/add", newLesson).then(
(response) => {
console.log(response);
updateFormData(initialFormData);
window.location = "/";
},
(error) => {
console.log(error);
}
);
};