Git Product home page Git Product logo

mayank0255 / stackoverflow-clone-backend Goto Github PK

View Code? Open in Web Editor NEW
117.0 7.0 64.0 4.15 MB

Backend code of the Stackoverflow Clone project. Built using Express, Node, MySQL, and Sequelize

Home Page: https://stackoverflow-clone-api.onrender.com

License: MIT License

Shell 0.49% JavaScript 99.16% Dockerfile 0.33% Procfile 0.02%
javascript nodejs api rest-api mysql express expressjs sql database stackoverflow stack backend mern-stack mern hacktoberfest sequelize docker

stackoverflow-clone-backend's Introduction

Stackoverflow-Clone-Backend

Version NPM NODE MYSQL License

API Hosted On

As the name suggests, this project is a clone of a famous Q/A website for professional and enthusiast programmers built solely by me using a completely different stack.

This repo consists of the Backend code of the project, the backend code is in Stackoverflow-Clone-Frontend

My Tech Stack (MERN)

Front-end

  • Front-end Framework: React.js (with Redux)
  • Styling: SASS and BOOTSTRAP

Back-end

  • For handling index requests: Node.js with Express.js Framework
  • As Database: MySQL with Sequelize
  • API tested using: POSTMAN

Guidelines to setup

There are two ways to setup the project: manually or using the Dockerfile. Read below for more details:

Manual Setup

  1. Open your local CLI -

    mkdir Stackoverflow-Clone
    cd Stackoverflow-Clone
    
  2. Setup the backend code -

    • Create a .env file and the format should be as given in .env.example.

    • Clone the code & install the modules-

      git clone https://github.com/Mayank0255/Stackoverflow-Clone-Backend.git
      cd Stackoverflow-Clone-Backend
      
      npm install
      
    • Open your MySQL Client -

      CREATE DATABASE stack_overflow;
      

      NOTE: Don't forget to keep the database name same in the .env and here.

    • Run the index npm start.

  3. Open a new CLI terminal and goto the root Stackoverflow-Clone folder you created in the first step.

  4. Setup the Frontend code -

    • Clone the code & install the modules-

      git clone https://github.com/Mayank0255/Stackoverflow-Clone-Frontend.git
      cd Stackoverflow-Clone-Frontend
      
      npm install
      
    • Run the client index npm start.

Docker Setup

The back-end has support for Docker. So if you want to run the back-end in a container, you need do:

  • Setup environment variables in .env file. Note when you use Docker setup and run the database in localhost (host machine), you need to setup the environment variables for use correct IP of MySQL Database. Please, read here and here for more details.

  • Build the Docker image:

    docker build -t stackoverflowclone .
    
  • Run the container. For example, if you want to run the container in a new terminal, you can do:

    docker run -d -p 5000:5000 stackoverflowclone
    

The default port of api is 5000. After running the container, you can access the api by typing:

http://localhost:5000/api/<endpoint that you request - see next section>

Follow the steps properly (manual or Docker) and you are good to go.

API Endpoints

API is currently hosted on stackoverflow-clone-api.onrender.com and stackoverflow-clone-backend.herokuapp.com.

You can view and read the API endpoints samples here. This is API documentation for the back-end.

But, if you want use Postman to test the API in local machine, you need to follow the steps below:

  • Get the Postman app from here.
  • Download the Postman collection file in folder "/data/postman_collection"
  • Import the collection file in Postman
  • Important: will be necessary to setup the enviroment with the "VARIABLE"=urlAPI and "INITIAL VALUE"=http://localhost:5000, for example.
  • Remember: keep the Postman collection updated with the latest API endpoints.

Base Url - {API_URL}/api

Users

  • GET /auth
  • POST /auth
  • POST /users/:id
  • GET /users
  • GET /users/:id

Posts

  • GET /posts
  • GET /posts/top
  • GET /posts/tag/:tagname
  • GET /posts/:id
  • POST /posts/
  • DELETE /posts/:id

Answers

  • GET /posts/answers/:id
  • POST /posts/answers/:id
  • DELETE /posts/answers/:id

Comments

  • GET /posts/comments/:id
  • POST /posts/comments/:id
  • DELETE /posts/comments/:id

Tags

  • GET /tags
  • GET /tags/:tag_name

Contributing

DEMO

Video Last Updated on 7th March, 2022

IMAGES

stackoverflow-clone-backend's People

Contributors

akgupta0777 avatar anangnovriadi avatar chaithanyapopcornapps avatar dhruv42 avatar fabioalmeida100 avatar jqueguiner avatar kmarrip avatar mayank0255 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

stackoverflow-clone-backend's Issues

[Docs]: make available Postman or Insomnia collection

My proposal: it'll be good have a collection with all endpoints and payload pre setup to new developers understand more fast all possibilities of request to back-end.

I have all endpoint setup in Postman collection, so if it's a good idea I can open a PR to make available it in Postman.

[BACKEND]: server error when registering a new user

Describe the bug
When registering a new user a server error occurs but the user is then created anyway

To Reproduce
Steps to reproduce the behavior:

  1. Click on "Sign Up" on the start page
  2. Enter a username and password
  3. Click on "Sign up"
  4. A red error appears stating "server error"

Expected behavior
The user should be created without an error

Additional context
The following gets logged by the running server

You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md
Error: You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md
at Query.then (/opt/Stackoverflow-Clone/node_modules/mysql2/lib/commands/query.js:41:11)
POST /api/users 500 66.327 ms - 64

The user gets created anyway and it is possible to login with it

[BUG]: sql files are incorrect

sql file is not same modle design

sql:
CREATE TABLE users(
id VARCHAR(255) PRIMARY KEY,
username VARCHAR(255) UNIQUE NULL,
password VARCHAR(100),
views INT DEFAULT 0,
gravatar VARCHAR(255) UNIQUE NULL,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
);

model:
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},

[Backend]: Add Docker support for development and production

To Helps with initial setup and ensures the integrity of the app

Add docker support for the development and production environment. Creating a Dockerfile and docker-compose files, Adding all the necessary environment like Mysql together with compose.

Speed Optimization

Hey Mayank0255!
I can implement Caching to for fast loading ,If you assign me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.