Polling System API using Node.js, Express.js and MongoDB
In this article, we will take a look at how to build a polling system API using Node.js, Express.js, and MongoDB.
Setting up the environment
Before we start building the API, make sure you have Node.js, Express.js, and MongoDB installed on your system. You can download and install Node.js from the official website and use npm to install Express.js. For MongoDB, you can download and install the community edition from the official website or use a cloud-based service like MongoDB Atlas.
Creating the project
Once you have all the dependencies installed, you can create a new directory for your project and initialize a new Node.js project using npm. Open a terminal and run the following commands:
mkdir polling-system-api
cd polling-system-api
npm init -y
npm install express mongoose body-parser
This will create a new directory for your project, initialize a new Node.js project, and install the required dependencies for building the API.
Building the API
Now that we have everything set up, we can start building the API. Create a new file called app.js
in the root of your project directory and add the following code:
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost/polling_system', { useNewUrlParser: true, useUnifiedTopology: true });
// Define the Polling Model
const Poll = mongoose.model('Poll', {
question: String,
options: [String],
votes: [Number]
});
// Define the API endpoints
app.get('/api/polls', async (req, res) => {
const polls = await Poll.find();
res.json(polls);
});
app.post('/api/polls', async (req, res) => {
const { question, options } = req.body;
const poll = new Poll({
question,
options,
votes: new Array(options.length).fill(0)
});
await poll.save();
res.status(201).send();
});
app.post('/api/polls/:id/vote', async (req, res) => {
const { id } = req.params;
const { option } = req.body;
const poll = await Poll.findById(id);
const index = poll.options.indexOf(option);
poll.votes[index]++;
await poll.save();
res.status(204).send();
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
This code sets up a basic Express.js server, connects to a local MongoDB database, defines a Poll model using Mongoose, and sets up three API endpoints for listing polls, creating new polls, and voting on polls.
Testing the API
To test the API, you can run the following commands in your terminal:
node app.js
This will start the Express.js server on port 3000. You can then use a tool like Postman to send requests to the API and test its functionality.
Conclusion
In this article, we have seen how to build a polling system API using Node.js, Express.js, and MongoDB. By following the steps outlined in this article, you can create a basic API for managing polls and allowing users to vote on them. From here, you can expand and enhance the API to add more features and functionality as needed.