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

Lesson 10 #36

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
.vscode/

# TypeScript v1 declaration files
typings/
Expand Down
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

91 changes: 91 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const Blog = require('./models/blog');

// express app
const app = express();

// connect to mongodb & listen for requests
const dbURI = "mongodb+srv://netninja:[email protected]/node-tuts";

mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(result => app.listen(3000))
.catch(err => console.log(err));

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

// middleware & static files
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.use((req, res, next) => {
res.locals.path = req.path;
next();
});

// routes
app.get('/', (req, res) => {
res.redirect('/blogs');
});

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

// blog routes
app.get('/blogs/create', (req, res) => {
res.render('create', { title: 'Create a new blog' });
});

app.get('/blogs', (req, res) => {
Blog.find().sort({ createdAt: -1 })
.then(result => {
res.render('index', { blogs: result, title: 'All blogs' });
})
.catch(err => {
console.log(err);
});
});

app.post('/blogs', (req, res) => {
// console.log(req.body);
const blog = new Blog(req.body);

blog.save()
.then(result => {
res.redirect('/blogs');
})
.catch(err => {
console.log(err);
});
});

app.get('/blogs/:id', (req, res) => {
const id = req.params.id;
Blog.findById(id)
.then(result => {
res.render('details', { blog: result, title: 'Blog Details' });
})
.catch(err => {
console.log(err);
});
});

app.delete('/blogs/:id', (req, res) => {
const id = req.params.id;

Blog.findByIdAndDelete(id)
.then(result => {
res.json({ redirect: '/blogs' });
})
.catch(err => {
console.log(err);
});
});

// 404 page
app.use((req, res) => {
res.status(404).render('404', { title: '404' });
});
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({
title: {
type: String,
required: true,
},
snippet: {
type: String,
required: true,
},
body: {
type: String,
required: true
},
}, { timestamps: true });

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