"This is a Voting API built using Node.js, Express, and MongoDB. It allows users to create polls, vote, and view results."
by Karan Kumar Gautam
02/03/2025
Welcome to this tutorial on building a Voting API using Node.js, Express, and MongoDB. This project demonstrates how to build a RESTful backend for managing voting polls β including creation, voting, and result viewing β in a clean and modular way.
The Voting API is a backend project that lets users:
This project is ideal for learning how REST APIs work, managing a MongoDB database using Mongoose, and structuring a scalable Node.js backend.
To run this API on your machine, follow these steps:
git clone https://github.com/gautamkaran/Voting-api.git
cd Voting-api
npm install
.env
FileAdd your MongoDB URI and port in a new .env
file:
PORT=5000
MONGO_URI=mongodb://localhost:27017/voting-api
npm start
The server will start on http://localhost:5000
Endpoint: POST /api/vote/create
Request Body:
{
"question": "What is your favorite frontend framework?",
"options": ["React", "Vue", "Angular", "Svelte"]
}
Endpoint: PUT /api/vote/:id
Request Body:
{
"option": "React"
}
Endpoint: GET /api/vote
Returns a list of all polls with their questions, options, and vote counts.
Voting-api/
β
βββ config/ # Database connection
β βββ db.js
βββ models/ # Mongoose models
β βββ Vote.js
βββ routes/ # API routes
β βββ voteRoutes.js
βββ server.js # Entry point
βββ .env # Environment variables
βββ package.json # Dependencies
βββ README.md
/api/vote/create
/api/vote/:id
/api/vote
returns the list of all polls with resultsKaran Kumar Gautam
π GitHub | π Portfolio
MIT β Use it freely in your projects
This Voting API project is a great way to get hands-on experience with REST APIs, MongoDB, and Node.js. Itβs clean, easy to extend, and ready to be used in real applications.
Happy coding!