const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectId;
const app = express();
// Port number where the server will listen
const PORT = 3030;
// Connection URL to the MongoDB instance
const url = 'mongodb://localhost:27017';
// Create a new MongoClient instance
const client = new MongoClient(url);
// Use the JSON middleware to parse incoming requests
app.use(express.json());
// Route to display a welcome message
app.get("/", (req, res) => {
res.send("Welcome to the Express MongoDB Example!");
});
// Route to retrieve all items from the 'items' collection
app.get('/items', async (req, res) => {
// Connect to the MongoDB server
await client.connect();
// Get the 'test' database
const db = client.db("test");
// Get the 'items' collection
const collection = db.collection('items');
// Retrieve all items from the collection and convert them to an array
const items = await collection.find({}).toArray();
// Send the items to the client
res.send(items);
});
// Route to retrieve a single item by its ID
app.get('/items/:id', async (req, res) => {
await client.connect();
const db = client.db("test");
const collection = db.collection('items');
const item = await collection.findOne({_id: new ObjectID(req.params.id)});
res.send(item);
});
// Route to insert a new item into the 'items' collection
app.post('/items', async (req, res) => {
await client.connect();
const db = client.db("test");
const collection = db.collection('items');
const item = { name: req.body.name, description: req.body.description };
const result = await collection.insertOne(item);
res.send(result);
});
// Route to update an existing item by its ID
app.put('/items/:id', async (req, res) => {
await client.connect();
const db = client.db("test");
const collection = db.collection('items');
const item = { name: req.body.name, description: req.body.description };
const result = await collection.updateOne({_id: new ObjectID(req.params.id)}, {$set: item});
res.send(result);
});
// Route to delete an existing item by its ID
app.delete('/items/:id', async (req, res) => {
await client.connect();
const db = client.db("test");
const collection = db.collection('items');
const result = await collection.deleteOne({_id: new ObjectID(req.params.id)});
res.send(result);
});
// Start the server and listen on the specified port
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = 3030;
const url = 'mongodb://localhost:27017/test';
// Connect to MongoDB using Mongoose
mongoose.connect(url, {useNewUrlParser: true});
const db = mongoose.connection;
// Check if the connection is successful
db.once('open', () => {
console.log('Connected to MongoDB');
});
// Create a Mongoose schema for the items
const itemSchema = new mongoose.Schema({
name: String,
description: String
});
// Create a Mongoose model for the items
const Item = mongoose.model('Item', itemSchema);
app.use(express.json());
// Route to display a welcome message
app.get("/", (req, res) => {
res.send("Welcome to the Express MongoDB Example!");
});
// Route to get all items
app.get('/items', async (req, res) => {
const items = await Item.find({});
res.send(items);
});
// Route to get a specific item
app.get('/items/:id', async (req, res) => {
const item = await Item.findById(req.params.id);
res.send(item);
});
// Route to create a new item
app.post('/items', async (req, res) => {
const item = new Item({ name: req.body.name, description: req.body.description });
await item.save();
res.send(item);
});
// Route to update an existing item
app.put('/items/:id', async (req, res) => {
const item = await Item.findByIdAndUpdate(req.params.id, { name: req.body.name, description: req.body.description }, { new: true });
res.send(item);
});
// Route to delete an item
app.delete('/items/:id', async (req, res) => {
const item = await Item.findByIdAndRemove(req.params.id);
res.send(item);
});
// Start the Express server
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});