packages and modules=
(a) npm init
(b) npm i node(it is for node packages)
(c) npm i mongoose(it is for database)
(d) npm i express(it is for backend)
(e) npm i nodemon(it is used to save the file automatically by pressing ctrl+s)
(f) npm i validator
(g) npm i hbs
(h) npm i jquery
(i) npm i bootstrap
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 1: src(folder) --->db(folder)+models(folder)+app.js(file)
step 2: db(folder) --->conn.js(file)
step3: models(folder)--->usermessage.js(file)
-----------------------------------------------------------------------------------
----------------------------------------------------------------
(imp)step 4 : first of all we can on the mongodb server by using mongod and mongosh
in cmd prompt
-----------------------------------------------------------------------------------
---------------------------------------------------------------
step 5:---> in db(folder)-->conn.js(file)
const mongoose = require('mongoose')
mongoose.set('strictQuery',false);
mongoose.connect("mongodb://localhost:27017/thapadynamiccccc",
{useNewUrlParser:true})
.then(()=>console.log('connection succesful....'))
.catch((err)=>console.log(err));
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 6:---->in src(folder)--->app.js(file)
const express = require('express')
const app = express();
require('./db/conn');
const port = process.env.PORT || 2000;
// routing
// app.get(path,callback)
app.get('/',(req,res)=>{
res.send('deepak')
//res.send("running")
});
// server create
app.listen(port, ()=>{
console.log(`server is running${port}`);
})
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 7:
public(folder)---->imges(folder)+new.html(file)+style.css(file)
the index.html file is only for check whether the code is perfectly run or not and
the style.css is for css code .
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 8:
const express = require('express');
const path = require('path');
const app = express();
require('./db/conn');
const port = process.env.PORT || 2000;
//setting the path
const staticpath = path.join(__dirname,"../public");
//midleware
app.use('/css',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
css')));
app.use('/js',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
js')));
app.use('/jq',express.static(path.join(__dirname,'../node_modules/jquery/dist/
jq')));
app.use(express.static(staticpath));
app.get('/',(req,res)=>{
res.render('index')
//res.send("running")
});
// server create
app.listen(port, ()=>{
console.log(`server is running${port}`);
})
->and now we can call path in app.js
->app.use it is used for bootstrap css,js,jq it is used when we are working on
bootstrap codes
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 9:
templates(folder)-->views(folder)1+partials(folder)
views(folder)-->index.hbs(it is the main root file for html code)
partials(folder)--->nav.hbs (it is partial file which can be used in root file by
{{>nav}} this syntax.
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 10:
const express = require('express');
const path = require('path');
const app = express();
require('./db/conn');
const User = require("./models/usermessage")
const hbs = require('hbs');
const port = process.env.PORT || 2000;
//setting the path
const staticpath = path.join(__dirname,"../public");
const templatepath = path.join(__dirname,"../templates/views");
const partialpath = path.join(__dirname,"../templates/partials");
//midleware
app.use('/css',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
css')));
app.use('/js',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
js')));
app.use('/jq',express.static(path.join(__dirname,'../node_modules/jquery/dist/
jq')));
app.set('view engine','hbs');
app.use(express.static(staticpath));
app.use(express.urlencoded({extended:false}));-->>(it is used for making output in
json format)
app.set('views',templatepath);
hbs.registerPartials(partialpath);
// routing
// app.get(path,callback)
app.get('/',(req,res)=>{
res.send('index')
//res.send("running")
});
// server create
app.listen(port, ()=>{
console.log(`server is running${port}`);
})
and then in terminal we can write[ nodemon src/app.js -e js,hbs] it is used for
handlebar files.
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 11: the last step is for form validation
models(folder)--->usermessages(file) it is already created
const mongoose = require('mongoose');
const validator = require('validator');
const userSchema = mongoose.Schema({
name:{
type:String,
required:true,
minLength:3
},
message:{
type:String,
required:true,
minLength:3
},
comments:{
type:String,
required:true,
minLength:1
}
})
// we need collection
const User = mongoose.model('User',userSchema);
module.exports = User;
-----------------------------------------------------------------------------------
----------------------------------------------------------------
step 12:
const express = require('express');
const path = require('path');
const app = express();
require('./db/conn');
const User = require("./models/usermessage")
const hbs = require('hbs');
const port = process.env.PORT || 2000;
//setting the path
const staticpath = path.join(__dirname,"../public");
const templatepath = path.join(__dirname,"../templates/views");
const partialpath = path.join(__dirname,"../templates/partials");
//midleware
app.use('/css',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
css')));
app.use('/js',express.static(path.join(__dirname,'../node_modules/bootstrap/dist/
js')));
app.use('/jq',express.static(path.join(__dirname,'../node_modules/jquery/dist/
jq')));
app.set('view engine','hbs');
app.use(express.static(staticpath));
app.use(express.urlencoded({extended:false}));
app.set('views',templatepath);
hbs.registerPartials(partialpath);
// routing
// app.get(path,callback)
app.get('/',(req,res)=>{
res.render('index')
//res.send("running")
});
app.post('/contact',async(req,res)=>{
try{
//res.send(req.body)
const userData = new User(req.body);
await userData.save();
res.status(201).render("index");
}catch(error){
res.status(500).send(error);
}
})
// server create
app.listen(port, ()=>{
console.log(`server is running${port}`);
})