{"id":86827,"date":"2024-12-05T01:05:41","date_gmt":"2024-12-04T21:35:41","guid":{"rendered":"https:\/\/nabfollower.com\/blog\/creating-a-server-and-connecting-it-to-a-database-like-mongodb-5e17\/"},"modified":"2024-12-05T01:05:41","modified_gmt":"2024-12-04T21:35:41","slug":"creating-a-server-and-connecting-it-to-a-database-like-mongodb-5e17","status":"publish","type":"post","link":"https:\/\/nabfollower.com\/blog\/creating-a-server-and-connecting-it-to-a-database-like-mongodb-5e17\/","title":{"rendered":"\u0627\u06cc\u062c\u0627\u062f \u06cc\u06a9 \u0633\u0631\u0648\u0631 \u0648 \u0627\u062a\u0635\u0627\u0644 \u0622\u0646 \u0628\u0647 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 \u0645\u0627\u0646\u0646\u062f MongoDB"},"content":{"rendered":"<p>Summarize this content to 400 words in Persian Lang <\/p>\n<p>\/\/ Import required modules<br \/>\nconst express = require(&#8216;express&#8217;);<br \/>\nconst mongoose = require(&#8216;mongoose&#8217;);<\/p>\n<p>\/\/ Initialize Express app<br \/>\nconst app = express();<\/p>\n<p>\/\/ Middleware for parsing JSON<br \/>\napp.use(express.json());<\/p>\n<p>\/\/ MongoDB Connection URI<br \/>\nconst mongoURI = &#8216;mongodb:\/\/localhost:27017\/myDatabase&#8217;; \/\/ Replace with your MongoDB URI<\/p>\n<p>\/\/ Connect to MongoDB<br \/>\nmongoose.connect(mongoURI, {<br \/>\n  useNewUrlParser: true,<br \/>\n  useUnifiedTopology: true,<br \/>\n})<br \/>\n  .then(() =&gt; {<br \/>\n    console.log(&#8216;Connected to MongoDB&#8217;);<br \/>\n  })<br \/>\n  .catch((err) =&gt; {<br \/>\n    console.error(&#8216;Error connecting to MongoDB:&#8217;, err);<br \/>\n  });<\/p>\n<p>\/\/ Basic Route<br \/>\napp.get(&#8220;https:\/\/dev.to\/&#8221;, (req, res) =&gt; {<br \/>\n  res.send(&#8216;Server is running and connected to MongoDB&#8217;);<br \/>\n});<\/p>\n<p>\/\/ Start the server<br \/>\nconst PORT = 3000;<br \/>\napp.listen(PORT, () =&gt; {<br \/>\n  console.log(`Server is running on http:\/\/localhost:${PORT}`);<br \/>\n});<\/p>\n<p>    \u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/p>\n<p>    \u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/p>\n<div data-article-id=\"2136886\" id=\"article-body\">\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ Import required modules\nconst express = require('express');\nconst mongoose = require('mongoose');\n\n\/\/ Initialize Express app\nconst app = express();\n\n\/\/ Middleware for parsing JSON\napp.use(express.json());\n\n\/\/ MongoDB Connection URI\nconst mongoURI = 'mongodb:\/\/localhost:27017\/myDatabase'; \/\/ Replace with your MongoDB URI\n\n\/\/ Connect to MongoDB\nmongoose.connect(mongoURI, {\n  useNewUrlParser: true,\n  useUnifiedTopology: true,\n})\n  .then(() =&gt; {\n    console.log('Connected to MongoDB');\n  })\n  .catch((err) =&gt; {\n    console.error('Error connecting to MongoDB:', err);\n  });\n\n\/\/ Basic Route\napp.get(\"https:\/\/dev.to\/\", (req, res) =&gt; {\n  res.send('Server is running and connected to MongoDB');\n});\n\n\/\/ Start the server\nconst PORT = 3000;\napp.listen(PORT, () =&gt; {\n  console.log(`Server is running on http:\/\/localhost:${PORT}`);\n});\n\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summarize this content to 400 words in Persian Lang \/\/ Import required modules const express = require(&#8216;express&#8217;); const mongoose = require(&#8216;mongoose&#8217;); \/\/ Initialize Express app const app = express(); \/\/ Middleware for parsing JSON app.use(express.json()); \/\/ MongoDB Connection URI const mongoURI = &#8216;mongodb:\/\/localhost:27017\/myDatabase&#8217;; \/\/ Replace with your MongoDB URI \/\/ Connect to MongoDB mongoose.connect(mongoURI, { &hellip;<\/p>\n","protected":false},"author":2,"featured_media":86828,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[339],"tags":[],"class_list":["post-86827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/86827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/comments?post=86827"}],"version-history":[{"count":0,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/86827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media\/86828"}],"wp:attachment":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media?parent=86827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/categories?post=86827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/tags?post=86827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}