- I required all the moduls i needed for this project
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
- Firts i coonected to the momgoDB Server using this path
/users/emmanuel/mongodb/bin/mongod --dbpath=/users/emmanuel/mongodb-data
-
Firts i coonected to the momgoDB Server using this path
-
This connect and Create todo-app-DB
mongoose.connect('mongodb://127.0.0.1:27017/todo-app-DB', {
useNewUrlParser: true,
useCreateIndex: true,//help us to quickly access our database
useFindAndModify: false
});
- This defines the structure of my application
//TODO SCHEMMA
const itemSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Description cannot be empty'],
min: 4
}
});
- This is used to create the instance of the todo
const Item = new mongoose.model('Item', itemSchema);
- To create real todo, create instance of the model
//Creating items
const item1 = new Item ({
name: 'Going Home'
})
const item2 = new Item ({
name: 'Learning'
})
const item3 = new Item ({
name: 'Dinner'
})
const item4 = new Item ({
name: 'lesson plan'
})
-
I created js function to display date of the day
-
I created a route to display the value
app.get('/', (req, res) => {
let today = new Date();
let options = {
weekday: 'long',
day: 'numeric',
month: 'long'
};
let day = today.toLocaleString('en-US', options)
//Rendering to webpage
res.render('list', {kindOfDay: day})
})
})
- The res.render() accept two arguements: 1. The ejs file which will display the value , 2. Objects.
res.render('list', {kindOfDay: day}) Means assign the value of day from the function above and assign it to a varriable called kindOfDay, This holds the value of the function.
After that i passed the the varriable kindOfDay to the ejs file for displaying
- To display any value to webpage pass it to this res.renderres.render('list', {kindOfDay: day, newListItem: foundItem
-
I created a post route. This will send request to a form for processing the data
-
I created a form to receive the request from the post route for the processing
app.post('/', (req, res) => {
const itemName = req.body.newItem;
const item = new Item ({
name: itemName
});
item.save()
res.redirect('/');
})
<form class="item" action="/" method="POST">
<input type="text" name="newItem" placeholder="Enter todo">
<button type="submit" name="list">+</button>
</form>
-
The input field has a name variable called newItem
-
When a user visit th above route which is the home route, a form will display, and when a user submit the form data, since the form has an action = '/' and method method="POST", it will move to that route, and any functions declared in that route will be executed.
-
When a user submit a form, the data from that field can be found in the route as and stored inside the itemName variable, This is achieved with the help of body-parser module
const itemName = req.body.newItem;
- The 'newItem' is the name varible from the field
- Grab the value from the form and assign it to a variable
const itemName = req.body.newItem;
-
Create instance of the model and assign the value from the form to respective properties of the model, after that redirect the user to homepage
-
Use this to save to the DB
item.save()
const item = new Item ({
name: itemName
});
res.redirect('/');
app.post('/', (req, res) => {
const itemName = req.body.newItem;
const item = new Item ({
name: itemName
});
item.save()
res.redirect('/');
})