<?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>DEV Community: Washington Kimani</title>
    <description>The latest articles on DEV Community by Washington Kimani (@kimaniwashington).</description>
    <link>https://dev.to/kimaniwashington</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%2F953657%2F7e4831de-1c4f-496d-9358-a949b7ef02e9.jpeg</url>
      <title>DEV Community: Washington Kimani</title>
      <link>https://dev.to/kimaniwashington</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kimaniwashington"/>
    <language>en</language>
    <item>
      <title>Create a simple Node.JS server(Express)in only 4 steps. I promise😄😄.</title>
      <dc:creator>Washington Kimani</dc:creator>
      <pubDate>Sun, 19 Feb 2023 11:55:47 +0000</pubDate>
      <link>https://dev.to/kimaniwashington/create-a-simple-nodejs-serverexpressin-only-4-steps-i-promise-2fhc</link>
      <guid>https://dev.to/kimaniwashington/create-a-simple-nodejs-serverexpressin-only-4-steps-i-promise-2fhc</guid>
      <description>&lt;p&gt;Hello coders! I hope y'all doing well. Today we're going to create a quick server using Express. Express is a Web Framework built upon Node.js.&lt;br&gt;
Node.js is an amazing tool for building networking services and applications. Express builds on top of its features to provide easy-to-use functionality that satisfies the needs of the Web Server use-case. It's Open Source, free, easy to extend and very efficient.&lt;/p&gt;

&lt;p&gt;Enough chit chat, let's get to it:&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 1. Install NodeJs on your machine.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For this exercise you will need to have NodeJs installed on your machine. This allows your machine to run JavaScript code outside the browser.&lt;br&gt;
Click on &lt;a href="https://nodejs.org/en/download/"&gt;this link&lt;/a&gt; to download NodeJs.&lt;br&gt;
Download the installation package and install it on your machine.&lt;br&gt;
To ensure successful installation, run the following command.&lt;br&gt;
&lt;code&gt;node --version&lt;/code&gt; or &lt;code&gt;node -v&lt;/code&gt;&lt;br&gt;
If the message &amp;gt; v18.13.0 is returned, then NodeJs is working just fine.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2. Create a project folder and initiate the node modules and packages.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;For this, go to your preferred directory and create a new folder. Then open the folder in your favorite code editor, preferably one that can access the command-line internally(Visual Studio Code).&lt;br&gt;
Open the VSCode's command-line by clicking "Terminal on top-bar" or us "CTRL + ~".(For Windows the default command-line will be Command prompt and for Mac and Linux users the command-line will be the Terminal).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IK8Asyw5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxqzp1cbt4s0msc727nw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IK8Asyw5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxqzp1cbt4s0msc727nw.png" alt="Image description" width="235" height="177"&gt;&lt;/a&gt;&lt;br&gt;
In the command-line, type the command,&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 -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new NodeJs project in your folder.&lt;br&gt;
NPM stands for Node Package Manager. It's a standard package manager for JavaScript projects.&lt;br&gt;
&lt;strong&gt;npm init&lt;/strong&gt;: Creates a package.json file for your project. If you’re building an application from scratch, npm init will be one of the first commands you use to include key project information.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 3. Install Express.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To install express, run the command,&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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straightforward right?😄😄&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 4. Create the Server.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To do this you will need to create a new JavaScript file, server.js(the filename is your choice😉).&lt;br&gt;
Inside the JavaScript file, write the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Server is open on port 5000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to the terminal and start up the server using,&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You can open the browser to port 5000 on localhost, "&lt;a href="http://127.0.0.1:5000"&gt;http://127.0.0.1:5000&lt;/a&gt; or &lt;a href="http://localhost:5000"&gt;http://localhost:5000&lt;/a&gt;" and you should see the "Hello World!" message.&lt;/p&gt;

&lt;p&gt;The above 4 lines of code do a lot behind the scenes.&lt;br&gt;
First, we import the express package to the express value.&lt;br&gt;
We instantiate an application by calling the express() method.&lt;br&gt;
Once we have the application object, we tell it to listen for GET requests on&lt;br&gt;
the / path, using the get() method.&lt;/p&gt;

&lt;p&gt;If you're familiar with JS callback functions you might have noticed that both app.get() and app.listen() accept a callback function as a parameter. These functions are called when a request is started - and we need to handle it.&lt;/p&gt;

&lt;p&gt;In the app.listen() method, there are two parameters. The first one is the server port on which the server is running.&lt;br&gt;
You can use a direct value or first initialize a valuable as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion.&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now, there is a method for every HTTP verb: get(), post(), put(), delete() , patch() :&lt;/p&gt;

&lt;p&gt;app.get('/', (req, res) =&amp;gt; { /* &lt;em&gt;/ })&lt;br&gt;
app.post('/', (req, res) =&amp;gt; { /&lt;/em&gt; &lt;em&gt;/ })&lt;br&gt;
app.put('/', (req, res) =&amp;gt; { /&lt;/em&gt; &lt;em&gt;/ })&lt;br&gt;
app.delete('/', (req, res) =&amp;gt; { /&lt;/em&gt; &lt;em&gt;/ })&lt;br&gt;
app.patch('/', (req, res) =&amp;gt; { /&lt;/em&gt; */ })&lt;/p&gt;

&lt;p&gt;We will look at each in the future. For now, PEACE coders✌️✌️!&lt;/p&gt;

&lt;p&gt;Thank you for reading, feel free to insight, correct, inspire and feel inspired. See you in the next one😉.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
