This project is a full-featured social network and blog application built with Node.js, Express.js, and MongoDB. It serves as the backend for a full-stack application, providing core functionalities such as user authentication, data storage, and CRUD operations. Users can sign up, create posts, and view other posts, making it an engaging platform for social interaction and content sharing. The project also includes features like pagination, file uploads, and integration with REST APIs and GraphQL (an option), making it a comprehensive example of a Node.js application in a real-world context.
Visit the deployed backend application:
Backend Deployment
Visit the deployed frontend application:
Frontend Deployment
Note: Due to free tier limitations, expect delays on first load due to app sleep mode.
Note: This project is derived from the Udemy course
NodeJS - The Complete Guide (MVC, REST APIs, GraphQL, Deno) by Maximilian Schwarzmüller.
Table of Contents
This Node.js REST API Blog project simulates a functional social network and blog application where users can sign up and create posts. It covers the essential features of web development, including:
- User Authentication: Secure user login and registration.
- CRUD Operations: Create, read, update, and delete blog posts.
- Data Persistence: Store data in a MongoDB database, using Mongoose for schema-based modeling.
- Error Handling: Proper error responses for different scenarios.
- Pagination: Efficiently handle large amounts of data by paginating blog post listings.
- File Uploads: Allow users to upload images for their blog posts.
- GraphQL Integration: Query and mutate blog data using GraphQL for more flexible APIs.
This project employs a wide range of technologies and libraries to ensure a robust, efficient backend for the blog platform.
- Node.js: Backend runtime to handle HTTP requests and data processing.
- Express.js: Web framework to set up routes and middleware.
- MongoDB: NoSQL database for storing user and resource data.
- Mongoose: ODM (Object Data Modeling) library for MongoDB and Node.js.
- JWT: For implementing secure authentication using JSON Web Tokens.
- Bcrypt.js: For hashing passwords.
- dotenv: For managing environment variables.
- Render: For deploying the application.
- axios: Promise-based HTTP client for making requests to APIs.
- body-parser: Middleware for parsing incoming request bodies in a middleware before your handlers, available under the
req.bodyproperty. - compression: Middleware to compress response bodies for all requests.
- express-validator: Middleware to validate and sanitize user inputs.
- jsonwebtoken: Library for generating and verifying JSON Web Tokens (JWT).
- multer: Middleware for handling
multipart/form-data, used for uploading files. - socket.io: Library for enabling real-time, bidirectional communication between web clients and servers.
- chai: Assertion library for Node.js and browser that can be paired with any testing framework.
- mocha: Test framework running on Node.js and in the browser to run asynchronous tests.
- nodemon: Utility that monitors for any changes in your source and automatically restarts your server.
- sinon: Library for creating spies, stubs, and mocks for JavaScript testing.
- JWT Authentication: Secure user login with JSON Web Tokens.
- Signup and Login: Users can register and log in securely.
- Access Control: Restricts certain actions to authenticated users only.
- Password Hashing: Passwords are stored securely using Bcrypt.js.
- Create Resource: Create new records in the database (e.g., users, blog posts).
- Read Resource: Retrieve stored data using GET requests.
- Update Resource: Modify existing data with PUT/PATCH requests.
- Delete Resource: Remove data from the database using DELETE requests.
- MongoDB: Stores user and resource data in a MongoDB database.
- Mongoose: Handles database operations and data validation.
- Error Responses: Provides proper error responses for different scenarios.
- Efficient Data Handling: Implements pagination for blog post listings to handle large amounts of data efficiently.
- REST API File Upload: Allows users to upload images for their blog posts via REST API.
- GraphQL File Upload: Enables file uploads using GraphQL for more flexible APIs.
- ImageBB Integration: Utilizes ImageBB for hosting and managing uploaded images.
- Flexible APIs: Allows querying and mutating blog data using GraphQL.
- Authentication in GraphQL APIs: Secure GraphQL endpoints using JWT for user authentication.
- Websockets Integration: Enables real-time updates and communication between clients and the server.
- Unit Tests: Implements unit tests using Mocha, Chai, and Sinon to ensure code reliability and correctness.
By completing this project, you will:
- Set up a Secure REST API: Implement user authentication using JSON Web Tokens (JWT) to secure API endpoints.
- Implement CRUD Operations: Create, read, update, and delete resources such as users and blog posts within a Node.js application.
- Work with MongoDB and Mongoose: Utilize MongoDB for data storage and Mongoose for schema-based modeling and data validation.
- Manage Environment Variables: Use dotenv to handle environment variables securely.
- Enable File Uploads: Implement file upload functionality using Multer and manage uploaded images with ImageBB.
- Integrate GraphQL: Set up a GraphQL server, define schemas and resolvers, and perform queries and mutations for flexible API interactions.
- Handle Errors Gracefully: Implement robust error handling to provide meaningful error messages and responses.
- Implement Pagination: Efficiently manage and display large data sets by implementing pagination.
- Use Real-Time Features with Websockets: Enable real-time updates and communication between clients and the server using Socket.io.
- Perform Automated Testing: Write and run unit tests using Mocha, Chai, and Sinon to ensure code reliability and correctness.
- Deploy a Node.js Application: Learn how to deploy your Node.js application using Render, ensuring it's accessible and running in a production environment.
- Node.js (v14.x or later)
- MongoDB: Local or Atlas instance.
- Clone the repository
git clone https://github.com/RamMichaeli17/Social-Blog.git- Install NPM packages
npm install- Set up environment variables - Create a
.envfile and add your credentials:
MONGO_DEFAULT_DATABASE=your_mongo_default_database
MONGO_PASSWORD=your_mongo_password
MONGO_USER=your_mongo_user
NODE_ENV=your_node_env
PORT=your_port
JWT_SECRET=your_jwt_secretFor the frontend project, create a .env file and include the following:
REACT_APP_API_BASE_URL=http://your-api-base-url.com- Start the server
npm startThis project allows users to interact with a fully functional blog platform. Here's what users can do:
- Register and Login: Users can sign up for a new account and log in to access personalized features.
- Create Blog Posts: Authenticated users can create new blog posts, adding titles, content, and images.
- Edit and Delete Blog Posts: Users can edit or delete their own posts, ensuring they have full control over their content.
- View Blog Posts: All users (authenticated or not) can view blog posts. The blog posts are paginated to handle large amounts of data efficiently.
- Comment on Posts: Authenticated users can add comments to blog posts, engage in discussions, and delete their own comments if necessary.
- Search Posts: Users can search for specific blog posts by keywords, making it easier to find content.
- GraphQL API: Developers can query and mutate blog data using the GraphQL API provided by the server.
Here's a breakdown of the steps involved in building the blog project:
- Project Initialization: Set up the Node.js project, install necessary dependencies (Express.js, MongoDB, etc.).
- Directory Structure: Organize folders and files for better project management.
- User Model: Create a User schema using Mongoose for MongoDB.
- Registration and Login: Implement routes and controllers for user signup and login.
- Authentication Middleware: Ensure certain routes are accessible only to authenticated users.
- Post Model: Create a schema for blog posts.
- CRUD Operations: Implement routes and controllers for Creating, Reading, Updating, and Deleting blog posts.
- User Associations: Link blog posts to users so they can only edit or delete their own posts.
- Comment Model: Create a schema for comments.
- Comment Routes: Implement functionality for users to add, view, and delete comments on blog posts.
- GraphQL Setup: Set up a GraphQL server using Apollo Server.
- Schemas and Resolvers: Define GraphQL schemas and resolvers for querying and mutating blog data.
- Testing GraphQL Queries: Use tools like GraphiQL to test your GraphQL API.
- File Uploads: Add functionality for users to upload images (e.g., for blog post thumbnails).
- Pagination: Implement pagination for blog post listings to handle large amounts of data.
- Search Functionality: Add a search feature to find blog posts by keywords.
- Environment Setup: Set up environment variables for production.
- Deploying: Deploy your application to a cloud service (e.g., Heroku, AWS).
Distributed under the MIT License. See LICENSE.txt for more information.
Thanks to the following people who have contributed to this project:
|
Ram Michaeli |
Ram Michaeli - [email protected]
Project Link: https://github.com/RamMichaeli17/Social-Blog
