Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ok #22

Open
wants to merge 1 commit into
base: lesson-8
Choose a base branch
from
Open

ok #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
const express = require('express');
const morgan = require('morgan');

const mongoose = require('mongoose');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const Blog = require('./models/blog');
const { result } = require('lodash');
// express app
const app = express();

//connect to mongodb
const dbURI = "mongodb+srv://shantanu:[email protected]/Shantanu?retryWrites=true&w=majority";
mongoose.connect(dbURI,{useNewUrlParser: true, useUnifiedTopology: true})
.then((result)=>app.listen(8000))
.catch((err)=> console.log(err))

app.set('view engine', 'ejs');
// listen for requests
app.listen(3000);
const connection


// register view engine
app.set('view engine', 'ejs');


// middleware & static files
app.use(express.static('public'));

app.use((req, res, next) => {
console.log('new request made:');
console.log('host: ', req.hostname);
console.log('path: ', req.path);
console.log('method: ', req.method);
next();
});

// app.use((req, res, next) => {
// console.log('new request made:');
// console.log('host: ', req.hostname);
// console.log('path: ', req.path);
// console.log('method: ', req.method);
// next();
// });
const connection = mongoose.createConnection(dbURI,{useNewUrlParser: true, useUnifiedTopology: true})
app.use((req, res, next) => {
console.log('in the next middleware');
next();
Expand All @@ -32,20 +44,39 @@ app.use((req, res, next) => {
res.locals.path = req.path;
next();
});
const store = new MongoDStore({
mongooseConnection: connection,
collection: 'sessions'
})
app.use(
session({
secret: 'flutter-viking-secret',

store: store,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 1000*60*60*24
}
})
);

app.get('/', (req, res) => {
const blogs = [
{title: 'Yoshi finds eggs', snippet: 'Lorem ipsum dolor sit amet consectetur'},
{title: 'Mario finds stars', snippet: 'Lorem ipsum dolor sit amet consectetur'},
{title: 'How to defeat bowser', snippet: 'Lorem ipsum dolor sit amet consectetur'},
];
res.redirect("/blogs")
res.render('index', { title: 'Home', blogs });
});

app.get('/about', (req, res) => {
res.render('about', { title: 'About' });
});

app.get("/blogs",(req,res)=>{
Blog.find().sort({createdAt:-1})
.then((result)=>{
res.render('index', { title: 'all BLogs', blogs: result });
}).catch((err)=>{
console.log(err)
})
})
app.get('/blogs/create', (req, res) => {
res.render('create', { title: 'Create a new blog' });
});
Expand Down
20 changes: 20 additions & 0 deletions models/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const blogSchema = new Schema({
tittle:{
tpye: String,

},
snippet:{
type: String,

},
body:{
type: String,

}
},{timestamps:true})

const Blog = mongoose.model("Blog", blogSchema);
module.exports = Blog;
Loading