Skip to content

Latest commit

 

History

History
118 lines (76 loc) · 3.57 KB

File metadata and controls

118 lines (76 loc) · 3.57 KB

Blog Api ✍️

Part 1

Instructions

Not Found Middleware

Create a middleware that handles requests to non-existing paths.

  1. Create a middleware method before the listen method.
  2. This middleware should end the response with a 404 status code and a message that says Path Not Found.
  3. Test your middleware by sending a request to a non-existing path, for example /whatever.

Error Handling Middleware

Create a middleware that handles all the errors in your application.

  1. Pass four arguments to your middleware's function: err, req, res and next.
  2. Set the response status to the error object's status. If it doesn't have a status, give it a default status of 500.
  3. Send an object with the message from the error object as a JSON response. If the error object doesn't have a message, give it the following default message Internal Server Error.

More Middlewares

Morgan Setup

  1. Install morgan
$ npm install morgan
  1. In app.js, create a variable called morgan.
  2. Configure your express application to use morgan using the dev format.

Cors

  1. Install cors
$ npm install cors
  1. In app.js, create a variable called cors.
  2. Enable your app to use the cors middleware.

Part 2

Setup Media Folder

Create a route for the media files.

  1. Create a folder called media for your images.
  2. In app.js, create a route with the path /media.
  3. Join media to the directory path __dirname using join and pass it to express.static.
  4. Test your route by putting any image in the media folder, then in your browser go to localhost:8000/media/<image_name>.
  5. Add media to your .gitignore file.

Setup Upload Middleware

Set up the upload middleware using multer.

  1. Install multer
$ npm install multer
  1. Create a directory called middleware in your root of your application directory, then within the middleware folder, create a file called multer.js.

  2. In this file multer.js, copy paste the following code:

const multer = require("multer");

const storage = multer.diskStorage({
  destination: "./media",
  filename: (req, file, cb) => {
    cb(null, `${+new Date()}${file.originalname}`);
  },
});

const upload = multer({
  storage,
});

module.exports = upload;
  1. In posts.routes.js, require the newly created multer middleware.
const upload = require("../../middleware/multer");
  1. Include the upload middleware before the postsCreate controller route.

  2. Specify that single images are uploaded only and the field name is image.

Uploading Images

  1. In postsCreate controller method in the posts.controllers.js, check if an image was uploaded by checking if req.file exists.
  2. If a file is uploaded, save the path in the body as req.body.image before the post instance is created.
  3. In the PostSchema, add a new property called image of type String.
  4. Test your post create api using postman.
  5. Try to access the image from the post through the browser.

Bonus

🍋 Slug Middleware

Create a custom middleware when creating a blog that generates a slug for the blog title and adds it to the request.

🌶 Validation Middleware

Use a validation middleware library.

Validate that the title argument contains letters and not only numbers, and does not exceed 40 character.