-
Install Node.js: Make sure you have Node.js installed on your machine. You can download it from nodejs.org.
-
Create a New Directory: Create a new directory for your project and navigate into it.
mkdir my-backend cd my-backend
-
Initialize a New Node.js Project: Run the following command to create a
package.json
file.npm init -y
Install the necessary packages for your backend.
npm install express mongoose body-parser cors
- express: A web framework for Node.js.
- mongoose: An ODM for MongoDB.
- body-parser: Middleware to parse incoming request bodies.
- cors: Middleware to enable Cross-Origin Resource Sharing.
-
Create a MongoDB Database: If you don't have a MongoDB database, you can create one using MongoDB Atlas or install MongoDB locally.
-
Get Your Connection String: If you're using MongoDB Atlas, get your connection string from the dashboard. It should look something like this:
mongodb+srv://<username>:<password>@cluster0.mongodb.net/mydatabase?retryWrites=true&w=majority
Create a file named server.js
in your project directory and add the following code:
// server.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
// Create an Express application
const app = express();
// Middleware
app.use(cors());
app.use(bodyParser.json());
// MongoDB connection
const mongoURI = 'YOUR_MONGODB_CONNECTION_STRING'; // Replace with your MongoDB connection string
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error(err));
// Define a schema and model
const DataSchema = new mongoose.Schema({
name: String,
value: Number,
});
const DataModel = mongoose.model('Data', DataSchema);
// Routes
app.post('/data', async (req, res) => {
const { name, value } = req.body;
const newData = new DataModel({ name, value });
try {
const savedData = await newData.save();
res.status(201).json(savedData);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get('/data', async (req, res) => {
try {
const data = await DataModel.find();
res.status(200).json(data);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
-
Start the Server: Run the following command in your terminal:
node server.js
-
Test Your API: You can use tools like Postman or curl to test your API.
-
POST Request: To add data, send a POST request to
http://localhost:5000/data
with a JSON body like:{ "name": "Sample Data", "value": 123 }
-
GET Request: To retrieve data, send a GET request to
http://localhost:5000/data
.
-
For security reasons, it's a good practice to store sensitive information like your MongoDB connection string in environment variables. You can use the dotenv
package for this.
-
Install dotenv:
npm install dotenv
-
Create a
.env
file in your project root and add your MongoDB connection string:MONGODB_URI=YOUR_MONGODB_CONNECTION_STRING
-
Update your
server.js
to use the environment variable:require('dotenv').config(); const mongoURI = process.env.MONGODB_URI;
You now have a basic backend set up with Node.js, Express, and MongoDB. You can expand this by adding more routes, validation, error handling, and other features as needed.