برنامه نویسی

ایجاد یک سرور و اتصال آن به پایگاه داده مانند MongoDB

Summarize this content to 400 words in Persian Lang

// Import required modules
const express = require(‘express’);
const mongoose = require(‘mongoose’);

// Initialize Express app
const app = express();

// Middleware for parsing JSON
app.use(express.json());

// MongoDB Connection URI
const mongoURI = ‘mongodb://localhost:27017/myDatabase’; // Replace with your MongoDB URI

// Connect to MongoDB
mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log(‘Connected to MongoDB’);
})
.catch((err) => {
console.error(‘Error connecting to MongoDB:’, err);
});

// Basic Route
app.get(“https://dev.to/”, (req, res) => {
res.send(‘Server is running and connected to MongoDB’);
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

وارد حالت تمام صفحه شوید

از حالت تمام صفحه خارج شوید

// Import required modules
const express = require('express');
const mongoose = require('mongoose');

// Initialize Express app
const app = express();

// Middleware for parsing JSON
app.use(express.json());

// MongoDB Connection URI
const mongoURI = 'mongodb://localhost:27017/myDatabase'; // Replace with your MongoDB URI

// Connect to MongoDB
mongoose.connect(mongoURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
  .then(() => {
    console.log('Connected to MongoDB');
  })
  .catch((err) => {
    console.error('Error connecting to MongoDB:', err);
  });

// Basic Route
app.get("https://dev.to/", (req, res) => {
  res.send('Server is running and connected to MongoDB');
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

وارد حالت تمام صفحه شوید

از حالت تمام صفحه خارج شوید

نوشته های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

دکمه بازگشت به بالا