<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kelechi Okoronkwo</title>
    <description>The latest articles on Forem by Kelechi Okoronkwo (@blakcoder).</description>
    <link>https://forem.com/blakcoder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F909064%2F26816659-0264-4d0c-a567-cd5f47d58e1f.jpeg</url>
      <title>Forem: Kelechi Okoronkwo</title>
      <link>https://forem.com/blakcoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/blakcoder"/>
    <language>en</language>
    <item>
      <title>I Created A Dev.to Clone With Nodejs, Express &amp; MongoDB.</title>
      <dc:creator>Kelechi Okoronkwo</dc:creator>
      <pubDate>Sun, 08 Jan 2023 22:42:50 +0000</pubDate>
      <link>https://forem.com/blakcoder/i-created-a-devto-clone-with-nodejs-1bc4</link>
      <guid>https://forem.com/blakcoder/i-created-a-devto-clone-with-nodejs-1bc4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
In this article I will be showing you how to create a Dev.to clone with Nodejs, Express, MongoDB, with JWT authentication. This API will give ability to users to create, read, update and delete blog alongside adding an authentication layer to it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pre-requisite&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NODEJS- You Should have node installed.&lt;/li&gt;
&lt;li&gt;MongoDB - You Should have mongoDB installed or have an account if you prefer to use the cloud version. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Packages that will be required&lt;/strong&gt;&lt;br&gt;
You can install the following using &lt;code&gt;npm&lt;/code&gt; or its equivalent&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "dependencies": {
    "bcryptjs": "^2.4.3",
    "cookie-parser": "^1.4.6",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "express-mongo-sanitize": "^2.2.0",
    "express-rate-limit": "^6.6.0",
    "helmet": "^6.0.0",
    "hpp": "^0.2.3",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^6.7.0",
    "morgan": "^1.10.0",
    "morgan-json": "^1.1.0",
    "nodemailer": "^6.8.0",
    "nodemon": "^2.0.20",
    "slugify": "^1.6.5",
    "validator": "^13.7.0",
    "winston": "^3.8.2",
    "xss-clean": "^0.1.1"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1 - File Structure&lt;/strong&gt;&lt;br&gt;
Before we write a line of code, lets create a &lt;code&gt;package.json&lt;/code&gt; file by running &lt;code&gt;npm init -y&lt;/code&gt; in our terminal. &lt;br&gt;
Now we would be using a file structure like the one below, which follows the MVC architecture. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fku4lw6cjjwo5575qpllo.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fku4lw6cjjwo5575qpllo.PNG" alt="Image description" width="189" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2 - Lets create our environmental variables in our &lt;code&gt;.env&lt;/code&gt; file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV=Developemt
PORT=8000
DATABASE_LOCAL=mongodb://127.0.0.1:27017/blogDB
DATABASE_PROD=mongodb+srv://blak:&amp;lt;password&amp;gt;@cluster0.1nb5crb.mongodb.net/blogdb?retryWrites=true&amp;amp;w=majority
DATABASE_PASSWORD=l3Ax3f4CaoJrKtjj
jwt_secret=mytokensecretblogapp20221026
jwt_expires=1h
jwt_cookie_expires=2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice I have two database URL, One for the local DB and the other for the cloud DB, You can pick your preference. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3 - Lets write some code in &lt;code&gt;server.js&lt;/code&gt; to start up our server and help us connect to our Database&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const app = require('./app');
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });

//CREATE DB CONNECTION
let DB = process.env.DATABASE_PROD.replace(
  '&amp;lt;password&amp;gt;',
  process.env.DATABASE_PASSWORD
);

if (process.env.NODE_ENV == 'Development') {
  DB = process.env.DATABASE_LOCAL;
}
mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then((con) =&amp;gt; {
    console.log('DB Connection Successful');
  });

//Connect To Server
const port = process.env.PORT || 8080;
const server = app.listen(port, () =&amp;gt; {
  console.log(`App running on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4 - Lets write some code in our &lt;code&gt;app.js&lt;/code&gt; file&lt;/strong&gt;&lt;br&gt;
This file contains code where all our middlewares will be registered, middlewares such as our route middlewares, global error handler middleware, some third-party middlewares for security and rate limiting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const UserRouter = require('./routes/userRoute');
const blogRouter = require('./routes/blogRoute');
const appError = require('./utils/appError');
const cookieParser = require('cookie-parser');
const globalErrHandler = require('./controllers/errorController');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const hpp = require('hpp');
const cors = require('cors');
const morganMiddleware = require('./utils/morgan');
const logger = require('./utils/logger');

//GLOBAL MIDDLEWARES
//Allow cross-origin access
app.use(cors());

//Set security HTTP headers
app.use(helmet());

const limiter = rateLimit({
  max: 500,
  windowMs: 24 * 60 * 60 * 1000,
  standardHeaders: true,
  message: 'Too Many Request From this IP, please try again in an hour',
});

//Set API Limit
app.use('/api', limiter);

//Data Sanitization against NOSQL query Injection
app.use(mongoSanitize());

//Data Sanitization against XSS
app.use(xss());

//Morgan Middleware
app.use(morganMiddleware);
//Allow views
app.use(express.static(`${__dirname}/views`));
app.use((req, res, next) =&amp;gt; {
  req.requestTime = new Date().toISOString();
  next();
});

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use('/api/v1/users', UserRouter);
app.use('/api/v1/articles', blogRouter);

//Global Error Handler
app.all('*', (req, res, next) =&amp;gt; {
  return next(
    new appError(404, `${req.originalUrl} cannot be found in this application`)
  );
});

app.use(globalErrHandler);
module.exports = app;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5 - Lets create our data models now.&lt;/strong&gt;&lt;br&gt;
Now that our server is up and connected to our DB, lets start modelling our data. we would start with user model, so lets head into the models folder and create a file called &lt;code&gt;userModel.js&lt;/code&gt; and paste the code below into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const validator = require('validator');
const bcrypt = require('bcryptjs');
const crypto = require('crypto');

const userSchema = new mongoose.Schema(
  {
    firstname: {
      type: String,
      required: [true, 'Please provide firstname'],
    },
    lastname: {
      type: String,
      required: [true, 'Please provide lastname'],
    },
    email: {
      type: String,
      required: [true, 'Please Provide Email Address'],
      unique: true,
      validate: [validator.isEmail, 'Please a valid email address'],
    },
    username: {
      type: String,
      unique: true,
    },
    password: {
      type: String,
      required: [true, 'Please Provide A Password'],
      minlength: 8,
      select: false,
    },
    passwordConfirm: {
      type: String,
      required: [true, 'Please Fill Password Field'],
      validate: function (el) {
        return el === this.password;
      },
      message: 'Password do not match.',
    },
    resetPasswordToken: String,
    resetTokenExpires: Date,
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);
userSchema.pre('save', async function (next) {
  if (!this.isModified('password')) return next();
  this.password = await bcrypt.hash(this.password, 12);
  this.passwordConfirm = undefined;
  this.username = this.username.toLowerCase();
  next();
});
userSchema.methods.createResetToken = function () {
  const resetToken = crypto.randomBytes(32).toString('hex');
  this.resetPasswordToken = crypto
    .createHash('sha256')
    .update(resetToken)
    .digest('hex');
  console.log({ resetToken }, this.resetPasswordToken);
  this.resetTokenExpires = Date.now() + 30 * 60 * 1000;
  return resetToken;
};
userSchema.methods.comparePassword = async function (
  signinPassword,
  userPassword
) {
  return await bcrypt.compare(signinPassword, userPassword);
};
//Virtual Properties To Load articles per user
userSchema.virtual('articles', {
  ref: 'Blog',
  foreignField: 'author',
  localField: '_id',
});
const User = mongoose.model('User', userSchema);

module.exports = User;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this file we create our model using mongoose and also used some mongoose hooks, methods &amp;amp; virtual properties  to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hash our users password before saving to the DB using &lt;code&gt;bcrypt.js&lt;/code&gt; package for hashing &amp;amp; unhashing.&lt;/li&gt;
&lt;li&gt;Create a reset token, when a user forgets his/her password.&lt;/li&gt;
&lt;li&gt;Add a reference to each blog created by a user. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 6 - Lets create the signup and login functionality&lt;/strong&gt;&lt;br&gt;
Head to the controller folder and create a file called &lt;code&gt;authController.js&lt;/code&gt;, and copy the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jwt = require('jsonwebtoken');
const catchAsy = require('./../utils/catchAsync');
const appError = require('./../utils/appError');
const User = require('./../models/userModel');
const sendEmail = require('./../utils/email');
const crypto = require('crypto');
const { promisify } = require('util');
const app = require('../app');
const Logger = require('../utils/logger');
const signToken = (id) =&amp;gt; {
  return jwt.sign({ id }, process.env.jwt_secret, {
    expiresIn: process.env.jwt_expires,
  });
};

const createToken = (user, statusCode, res) =&amp;gt; {
  const token = signToken(user._id);
  const cookieOptions = {
    expires: new Date(
      Date.now() + process.env.jwt_cookie_expires * 60 * 60 * 1000
    ),
    httpOnly: true,
  };
  if (process.env.NODE_ENV === 'production') cookieOptions.secure = true;

  //Send Token To Client
  res.cookie('jwt', token, cookieOptions);

  //Remove Password From JSON
  user.password = undefined;

  //Send Response To Client
  res.status(statusCode).json({
    status: 'Success',
    data: {
      token,
      user,
    },
  });
};
exports.signup = catchAsy(async (req, res, next) =&amp;gt; {
  const user = await User.create(req.body);

  createToken(user, 200, res);
});
exports.login = catchAsy(async (req, res, next) =&amp;gt; {
  const { email, password } = req.body;

  //If user does not provide any of the required fields
  if (!email || !password) {
    return next(new appError(401, 'Please Provide Email or Password.'));
  }

  const user = await User.findOne({ email }).select('+password');

  //If Any Field Is Incorrect
  if (!user || !(await user.comparePassword(password, user.password))) {
    const message = 'Email or Password Incorrect';
    return next(new appError(401, message));
  }

  //If everything checks out, send JWT Token.
  Logger.info(`New User Logged in with the ID of ${user.id}`);
  createToken(user, 200, res);
});

exports.forgetPassword = catchAsy(async (req, res, next) =&amp;gt; {
  //1.Get User From Posted Email
  const user = await User.findOne({ email: req.body.email });

  if (!user) return next(new appError(404, 'Email Address Not Found!'));

  //2. Create Token and Save to DB
  const resetToken = user.createResetToken();
  await user.save({ validateBeforeSave: false });

  //3. Send to Client
  const resetUrl = `${req.protocol}://${req.get(
    'host'
  )}/api/v1/users/resetpassword/${resetToken}`;
  const message = `You made a request for a password reset, Click on the link to reset your password, reset token is valid for 30mins! ${resetUrl} \n Please Ignore if you did not make this request`;

  try {
    sendEmail({
      email: user.email,
      subject: `Your Password Reset Token(Valid 30min)`,
      message,
    });
    res.status(201).json({
      status: 'Success',
      message: 'Please check inbox for reset token!',
    });
  } catch (err) {
    user.resetPasswordToken = undefined;
    user.resetTokenExpires = undefined;
    return next(
      new appError(
        500,
        'There was an error sending mail, Please try again later!'
      )
    );
  }
});

exports.resetPassword = catchAsy(async (req, res, next) =&amp;gt; {
  //1. Grab token from resetUrl
  const hashedToken = crypto
    .createHash('sha256')
    .update(req.params.token)
    .digest('hex');

  const user = await User.findOne({
    resetPasswordToken: hashedToken,
    resetTokenExpires: { $gt: Date.now() },
  });
  //2. Check IF token matches &amp;amp; still valid
  if (!user)
    return next(
      new appError(
        400,
        'Token Invalid or Expired, Try to reset password again!'
      )
    );
  //3. IF Token is valid
  user.password = req.body.password;
  user.passwordConfirm = req.body.passwordConfirm;
  user.resetPasswordToken = undefined;
  user.resetTokenExpires = undefined;
  await user.save();
  //4. Login User
  createToken(user, 200, res);
});

exports.protectRoute = catchAsy(async (req, res, next) =&amp;gt; {
  const token = req.cookies.jwt;
  if (!token) return next(new appError(400, 'Please Login Again!'));

  const decoded = await promisify(jwt.verify)(token, process.env.jwt_secret);
  //Check if user exists
  const currentUser = await User.findById(decoded.id);
  //if (decoded.expiresIn &amp;gt; Date.now() + jwt_cookie_expires * 60 * 60 * 1000)
  if (!currentUser)
    return next(new appError(404, 'Session expired, Login again!'));
  //Add user to req object
  req.user = currentUser;
  next();
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we allow users to register and login and also issue a JWT after a successful login. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7 - Lets mount our routes&lt;/strong&gt;&lt;br&gt;
Head to the route folder and create a file called &lt;code&gt;userRoute.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const authController = require('./../controllers/authController');
const router = express.Router();

router.post('/signup', authController.signup);
router.post('/login', authController.login);
router.post('/forgotPassword', authController.forgetPassword);
router.patch('/resetpassword/:token', authController.resetPassword);

module.exports = router;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lets test what we have now but lets configure some scripts in &lt;code&gt;package.json&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can go ahead to test our project now by using &lt;code&gt;npm start&lt;/code&gt; in our terminal and if you wrote the code correctly users should be able to create an account and login now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8 - Lets create our blog model&lt;/strong&gt;&lt;br&gt;
Now we are done with the users end, lets work on creating the model for the blog itself. Head to the models folder and create another file called &lt;code&gt;blogModel.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const validator = require('validator');
const slugify = require('slugify');

const blogSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: [true, 'Your Blog Requires A Title'],
      trim: true,
      unique: true,
    },
    slug: {
      type: String,
    },
    description: {
      type: String,
      required: [true, 'Please add a description'],
      trim: true,
    },
    author: {
      type: mongoose.Schema.ObjectId,
      ref: 'User',
    },
    state: {
      type: String,
      enum: ['draft', 'published'],
      default: 'draft',
    },
    read_count: {
      type: Number,
      default: 0,
    },
    reading_time: {
      type: String,
      default: 0,
    },
    tags: {
      type: [String],
    },
    body: {
      type: String,
      required: [true, 'Your blog must have a body!'],
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);
blogSchema.set('timestamps', true);
blogSchema.pre(/^find/, function (next) {
  this.populate({
    path: 'author',
    select: '-__v -resetPasswordToken -resetTokenExpires -password -email',
  });
  next();
});
blogSchema.pre('save', function (next) {
  this.slug = slugify(this.title, { lower: true });
  this.tags.forEach((el) =&amp;gt; {
    el.toLowerCase();
  });
  next();
});
blogSchema.methods.updateRead = async function () {
  this.read_count = this.read_count + 1;
  return this.read_count;
};
blogSchema.methods.calcReadTime = async function () {
  let words = this.title.length + this.body.length;
  let time = words / 200;
  const fullTime = time.toString().split('.');
  const min = fullTime[0];
  const sec = Math.round((fullTime[1] * 60) / 1000);
  return [min, sec];
};
const Blog = mongoose.model('Blog', blogSchema);

module.exports = Blog;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this file we create our model using mongoose and also used some mongoose hooks, methods to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Slugify blog titles. &lt;/li&gt;
&lt;li&gt;Calculate the read-time of each blog post.&lt;/li&gt;
&lt;li&gt;Update the read count of each blog. &lt;/li&gt;
&lt;li&gt;Add a reference to the author who created the blog. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 9 - Lets create functionalities for the blog&lt;/strong&gt;&lt;br&gt;
Head to the controller folder and create a file called &lt;code&gt;blogController.js&lt;/code&gt;, and copy the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Blog = require('./../models/blogModel');
const User = require('./../models/userModel');
const catchAsy = require('./../utils/catchAsync');
const appError = require('./../utils/appError');
const slugify = require('slugify');

exports.createBlog = catchAsy(async (req, res, next) =&amp;gt; {
  req.body.author = req.user._id;
  const blog = await Blog.create(req.body);
  const readTime = await blog.calcReadTime();
  blog.reading_time = `${readTime[0]} min ${readTime[1]} seconds`;
  await blog.save({ validateBeforeSave: false });

  res.status(200).json({
    status: 'Success',
    data: {
      blog,
    },
  });
});

exports.getAllBlogs = catchAsy(async (req, res, next) =&amp;gt; {
  //1. Create a query
  let query = Blog.find();

  //2. Check if user queries for any blog in draft state.
  if (req.query.state == 'draft') {
    return next(new appError(403, 'You cannot access unpublished articles!'));
  } else {
    query = Blog.find(req.query);
  }

  //Build query for author
  if (req.query.author) {
    const author = req.query.author;
    const user = await User.findOne({ username: author });
    if (!user)
      return next(
        new appError(403, 'Author does not exists or has written no articles')
      );
    const ID = user.id;
    query = Blog.find({ author: ID });
  }

  //Build query for tags
  if (req.query.tag) {
    const tag = req.query.tag.split(',');
    query = Blog.find({ tags: tag });
  }

  //Build Query For sort
  if (req.query.sort) {
    const sort = req.query.sort || 'createdAt';
    query = query.sort(sort);
  }
  //.Add Pagination
  const page = req.query.page * 1 || 1;
  const limit = req.query.limit * 1 || 20;
  const skip = (page - 1) * limit;
  query = query.skip(skip).limit(limit);

  //Await Query, and filter drafts out.
  const blog = await query;
  let newblog = [];
  if (blog.length == 0) return next(new appError(403, 'No Blog Found'));
  blog.forEach((el) =&amp;gt; {
    if (el.state == 'published') {
      newblog.push(el);
    }
  });

  res.status(200).json({
    status: 'success',
    result: newblog.length,
    data: {
      newblog,
    },
  });
});

exports.getBlog = catchAsy(async (req, res, next) =&amp;gt; {
  const ID = req.params.id;
  const blog = await Blog.findById(ID);

  if (blog.state == 'draft') {
    return next(new appError(403, 'You cannot access unpublished blog'));
  }
  const count = blog.updateRead();
  await blog.save({ validateBeforeSave: false });
  res.status(200).json({
    status: 'Success',
    data: {
      blog,
    },
  });
});

exports.updateBlog = catchAsy(async (req, res, next) =&amp;gt; {
  //1. Get Blog Id and Perform search in DB
  const blogID = req.params.id;
  const blog = await Blog.findById(blogID);

  //2. Return error when blog cannot be found
  if (!blog) return next(new appError(404, 'No Blog Found'));

  //3. Check if user is owner of blog
  if (blog.author.id === req.user.id) {
    //4. If everything checks out, allow user edit blog.
    const newBlog = await Blog.findByIdAndUpdate(blogID, req.body, {
      new: true,
      runValidators: true,
    });
    //5. Return data to user
    res.status(200).json({
      status: 'success',
      data: {
        newBlog,
      },
    });
  } else {
    return next(new appError(403, 'Action Forbidden, You cannot Update blog'));
  }
});

exports.deleteBlog = catchAsy(async (req, res, next) =&amp;gt; {
  //1. Get Blog Id and Perform search in DB
  const blogID = req.params.id;
  const blog = await Blog.findById(blogID);

  //2. Return error when blog cannot be found
  if (!blog) return next(new appError(404, 'No Blog Found'));

  //3. Check if user is owner of blog
  if (blog.author.id === req.user.id) {
    //4. If everything checks out, allow user delete blog.
    const newBlog = await Blog.findByIdAndDelete(blogID);
    //5. Return data to user
    res.status(204).json({
      status: 'success',
      data: null,
    });
  } else {
    return next(new appError(403, 'Action Forbidden, You cannot delete blog'));
  }
});

exports.myBlog = catchAsy(async (req, res, next) =&amp;gt; {
  const queryObj = { ...req.query };
  const excludedFields = ['page', 'sort', 'limit', 'fields'];
  excludedFields.forEach((el) =&amp;gt; delete queryObj[el]);

  //1. Grab user ID from protect route
  const userID = req.user.id;

  //2. Use ID To find Blog where it matches the author ID.
  let query = Blog.find({ author: userID });

  //3. Build Query For Other Query
  if (req.query.state) {
    const state = req.query.state;
    query = Blog.find({ author: userID, state: state });
  }
  //4.Add Pagination
  const page = req.query.page * 1 || 1;
  const limit = req.query.limit * 1 || 5;
  const skip = (page - 1) * limit;
  query = query.skip(skip).limit(limit);
  const blog = await query;

  res.status(200).json({
    status: 'success',
    result: blog.length,
    data: {
      blog,
    },
  });
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we configure all the functionalities of our blog &lt;em&gt;go through the comments in the code body&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 10 - Lets mount our routes for the blog&lt;/em&gt;*&lt;br&gt;
Head to the route folder and create a file called &lt;code&gt;blogRoute.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const blogController = require('./../controllers/blogController');
const authController = require('./../controllers/authController');

const router = express.Router();
router
  .route('/myblogs')
  .get(authController.protectRoute, blogController.myBlog);

router
  .route('/')
  .post(authController.protectRoute, blogController.createBlog)
  .get(blogController.getAllBlogs);

router.route('/:id').get(blogController.getBlog);

router
  .route('/update/:id')
  .patch(authController.protectRoute, blogController.updateBlog);

router
  .route('/delete/:id')
  .delete(authController.protectRoute, blogController.deleteBlog);
module.exports = router;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the major functionalities of our application are done, lets just add some utilities to make our app perfect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Class Util&lt;/strong&gt;&lt;br&gt;
Head to the util folder and create a file called &lt;code&gt;appError.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class appError extends Error {
  constructor(statusCode, message) {
    super(message);

    this.statusCode = statusCode;
    this.status = `${statusCode}`.startsWith('4') ? 'failed' : 'Server Error';
    this.isOperational = true;
    Error.captureStackTrace(this, this.constructor);
  }
}

module.exports = appError;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make this appError util work we need a corresponding controller file, so lets create a new controller called &lt;code&gt;errorController.js&lt;/code&gt; and copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const appError = require('./../utils/appError');

const handleCastErrorDB = (err) =&amp;gt; {
  const message = `Invalid ${err.path}: ${err.value}`;
  return new appError(400, message);
};
const handleDuplicateKeyDB = (err) =&amp;gt; {
  const value = err.keyValue.email;
  const message = `Duplicate field value: '${value}' Please use another value`;
  return new appError(400, message);
};
const handleValidationErrorDB = (err) =&amp;gt; {
  const errors = Object.values(err.errors).map((el) =&amp;gt; el.message);
  const message = `Invalid field data ${errors.join('. ')} Critical Error`;
  return new appError(400, message);
};
const handleJWTError = (err) =&amp;gt;
  new appError(401, 'Invalid token. Please login again');
const handleJWTExpiredError = (err) =&amp;gt;
  new appError(401, 'Your token is expired, Please login again');

const errorDev = (err, res) =&amp;gt; {
  res.status(err.statusCode).json({
    status: err.status,
    message: err.message,
  });
};
const errorProd = (err, res) =&amp;gt; {
  if (err.isOperational) {
    res.status(err.statusCode).json({
      status: err.status,
      message: err.message,
    });
    //Programming Errors or other unknown Error: Don't leak error details
  } else {
    console.error('ERROR 💣', err);
    res.status(500).json({
      status: 'error',
      message: 'Something went very wrong',
    });
  }
};

module.exports = (err, req, res, next) =&amp;gt; {
  err.statusCode = err.statusCode || 500;
  err.status = err.status || 'Server Error';
  if (process.env.NODE_ENV == 'Development') {
    console.log(err);
    errorDev(err, res);
  } else if (process.env.NODE_ENV == 'production') {
    let error = { ...err };
    error.message = err.message;
    if (error.name === 'CastError') error = handleCastErrorDB(error);
    if (error.code === 11000) error = handleDuplicateKeyDB(error);
    if (error._message === 'Validation failed')
      error = handleValidationErrorDB(error);
    if (error.name === 'JsonWebTokenError') error = handleJWTError(error);
    if (error.name === 'TokenExpiredError')
      error = handleJWTExpiredError(error);
    errorProd(error, res);
    console.log(err);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have successfully setup a robust way of handling both development and production error. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Catch async Util&lt;/strong&gt;&lt;br&gt;
Head to the util folder and create a file called &lt;code&gt;catchAsync.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = (fn) =&amp;gt; {
    return (req, res, next) =&amp;gt; {
        fn(req, res, next).catch(next);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code block is to handle the repeated use of try and catch block, thereby making our code more readable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Email Service Util&lt;/strong&gt;&lt;br&gt;
Head to the util folder and create a file called &lt;code&gt;email.js&lt;/code&gt;, copy the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nodemailer = require('nodemailer')

const sendEmail = async (options) =&amp;gt; {
    const transporter = nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
          user: process.env.EMAIL_USERNAME,
          pass: process.env.EMAIL_PASSWORD,
        },
    })

    const mailOptions = {
        from: 'Kelechi Okoronkwo &amp;lt;hello@blakcoder.tech&amp;gt;',
        to: options.email,
        subject: options.subject,
        text: options.message
    }
    await transporter.sendMail(mailOptions)
}
module.exports = sendEmail
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This util is to help in sending emails to users when requested, for example when users request for a password reset. Finally to make our email service work we would need to update our &lt;code&gt;.env&lt;/code&gt; file with the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EMAIL_USERNAME='4602f30010ad95'
EMAIL_PASSWORD='1f4568221277a4'
EMAIL_HOST='smtp.mailtrap.io'
EMAIL_PORT=2525

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I made use of &lt;em&gt;mailtrap&lt;/em&gt; for email testing, you can get the following details by creating an account on &lt;a href="//mailtrap.io"&gt;mailtrap.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally you can test and deploy to your favorite platform&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the complete source code &lt;a href="https://github.com/anslemkelechi/Blog-API" rel="noopener noreferrer"&gt;Source Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers&lt;br&gt;
May the force be with you. &lt;/p&gt;

</description>
      <category>startup</category>
      <category>discuss</category>
      <category>career</category>
    </item>
    <item>
      <title>Progressing As A Junior Developer.</title>
      <dc:creator>Kelechi Okoronkwo</dc:creator>
      <pubDate>Mon, 22 Aug 2022 12:37:37 +0000</pubDate>
      <link>https://forem.com/blakcoder/progressing-as-a-junior-developer-bio</link>
      <guid>https://forem.com/blakcoder/progressing-as-a-junior-developer-bio</guid>
      <description>&lt;p&gt;'Little droplets of water make a mighty ocean', 'Start Small and Scale'.&lt;br&gt;
These are phrases we often hear about life generally, but how do these relate to the tech industry &amp;amp; Programming in particular?&lt;/p&gt;

&lt;p&gt;Software Engineering is a broad field with constant change, so it's difficult for beginners to keep afloat and not get discouraged.&lt;br&gt;
With new technologies at the door of being shipped out, here are some tips to help you progress and maintain relevance in the industry:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always be open to learning:&lt;/strong&gt; When it comes to programming, satisfaction is not an option, never think you have known it all. You would always work with a team of developers, this gives you the platform to learn from others. Always remember "Alone we can do so little, together we can do so much"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Follow structured courses, not random tutorials:&lt;/strong&gt; Following a well-structured online course provides more value than picking up random tutorials because most of these courses are structured to take your knowledge of the language from basic to advanced while building small projects along the way, random tutorials can be good if you want to learn particular concepts, asides that focus on structured courses. Here is a link to a well-taught JavaScript course taught by Jonas Schmedtmann. &lt;a href="https://www.udemy.com/course/the-complete-javascript-course/"&gt;The complete javascript course&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Contribute to open source:&lt;/strong&gt; It’s never too early to start contributing to open source projects. Open source projects provide the platform to test your knowledge in building real-world applications. Whenever you learn a new concept try testing it out by collaborating on an open source project that requires a such skill set. Here is a resource on where to get engaging open source projects to contribute to &lt;a href="https://opensource.guide/how-to-contribute/"&gt;Opensource-guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Build, Build &amp;amp; Build:&lt;/strong&gt; The only reliable way of testing your knowledge is by building projects, don't just watch tutorials alone, try to implement what you have learned in multiple ways. Try rebuilding the project taught in the tutorials without consulting the material itself. Try building something different using the knowledge gained from the tutorial, this helps determine if truly the concepts explained have been understood.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Be open to failures:&lt;/strong&gt; Yes, you read that right! Studies have shown that when we repeatedly fail, our brains develop faster using the information from previous failures to avoid future recurrence. So it's okay not to understand the concepts at first read.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Maintain Grit &amp;amp; Keep at it!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;"Success lies in doing the simple things well enough to succeed"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Cover Image from &lt;a href="https://www.pexels.com/photo/man-wearing-blue-shirt-standing-on-cliff-while-watching-mountain-2450296/"&gt;DreamLens Production&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>motivation</category>
    </item>
  </channel>
</rss>
