<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Arslan</title>
    <description>The latest articles on Forem by Arslan (@im_arslan).</description>
    <link>https://forem.com/im_arslan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1262488%2F5520b7cf-63f0-4626-b690-5c816efea270.jpg</url>
      <title>Forem: Arslan</title>
      <link>https://forem.com/im_arslan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/im_arslan"/>
    <language>en</language>
    <item>
      <title>How to create and setup a Node.js server port 🌐</title>
      <dc:creator>Arslan</dc:creator>
      <pubDate>Sun, 21 Jan 2024 22:26:17 +0000</pubDate>
      <link>https://forem.com/im_arslan/how-to-create-and-setup-a-nodejs-server-port-3fmk</link>
      <guid>https://forem.com/im_arslan/how-to-create-and-setup-a-nodejs-server-port-3fmk</guid>
      <description>&lt;h1&gt;
  
  
  What is Node.js?
&lt;/h1&gt;

&lt;p&gt;Node.js is an open-source, cross-platform, back-end JavaScript runtime environment designed to execute JavaScript code outside a web browser. It allows developers to build scalable network applications using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Node and npm
&lt;/h2&gt;

&lt;p&gt;First, you have to download &lt;a href="https://nodejs.org/en/download/"&gt;Node &lt;/a&gt;and &lt;a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"&gt;npm &lt;/a&gt;from these links.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get package.json file
&lt;/h2&gt;

&lt;p&gt;Create a folder for your app, add a &lt;code&gt;server.js&lt;/code&gt; file inside, and navigate to that folder. Run npm init in the terminal, providing information like version and description. Make sure to set &lt;code&gt;server.js&lt;/code&gt; as the entry point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Install dependencies
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;Install Express (a Node.js framework for building RESTful APIs), Mongoose (a library for connecting MongoDB and Node.js), and Nodemon (a tool for restarting the server on file changes)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express mongoose
npm install -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Write a script
&lt;/h2&gt;

&lt;p&gt;Add the following scripts to your package.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server.js",
  "server": "nodemon server.js"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a port
&lt;/h2&gt;

&lt;p&gt;Now, in the end, type this code in your server.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const port = 8000;
const app = express();

app.listen(port, () =&amp;gt; {
  console.log(`Server started on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and to get the results, run this command in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get the running port in the terminal just like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5t0llr3uepr9bdbuzojv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5t0llr3uepr9bdbuzojv.png" alt="Image description" width="716" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This setup lets you quickly create a Node.js server using Express, connect to MongoDB using Mongoose, and automatically restart the server during Nodemon development.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Keep coding, keep sharing, stay motivated. The source code of success lies in persistence, passion, and a dash of creativity. Happy coding journey!”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thank you for reading! 😍&lt;br&gt;
Feel free to connect on &lt;a href="https://www.linkedin.com/in/arslan-malik-/"&gt;Linkedin&lt;/a&gt; ✨&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
