Full Website
Full Website
// 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}`);
})
// 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');
})
// 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}`);
})