<?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: Juan Moreno</title>
    <description>The latest articles on DEV Community by Juan Moreno (@moreno_dev).</description>
    <link>https://dev.to/moreno_dev</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%2F757596%2F71e38abf-d3fb-43be-bb88-f1301aea9cfc.jpeg</url>
      <title>DEV Community: Juan Moreno</title>
      <link>https://dev.to/moreno_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moreno_dev"/>
    <language>en</language>
    <item>
      <title>Your first RESTful route with Node and Express</title>
      <dc:creator>Juan Moreno</dc:creator>
      <pubDate>Sat, 15 Jan 2022 10:47:11 +0000</pubDate>
      <link>https://dev.to/moreno_dev/your-first-restful-route-with-node-and-express-3l5e</link>
      <guid>https://dev.to/moreno_dev/your-first-restful-route-with-node-and-express-3l5e</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hi all, today we'll be looking at a simple and beginner-friendly way to create your first RESTful route with Node and Express.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For those of you who aren't familiar with Node and Express. Node.js is simply an open source javascript web server environment that lets developers write command line tools and server-side scripts outside of a browser - (&lt;a href="https://nodejs.dev/learn"&gt;Learn more&lt;/a&gt;) couple that with Express.js &lt;a href="https://expressjs.com/en/starter/installing.html"&gt;Learn more&lt;/a&gt;, which is a backend web framework for Node that allows for setting up routes, middleware and dynamically render HTML pages. &lt;/p&gt;

&lt;p&gt;In this tutorial we will be creating our first route using Node and Express to render a greeting, "Hello, World!" to a web page.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To Start&lt;/strong&gt;&lt;br&gt;
You should have some familiarity with a code editor or IDE (Integrated Development Environment) and have Node.js installed on your machine. I will be using VS Code for this demonstration. To find out how to install Node.js check out the docs here &lt;a href="https://nodejs.dev/learn/how-to-install-nodejs"&gt;Installing node&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's Begin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open up VS Code or your favorite IDE and create a folder, name it whatever you'd like and we'll create a file named "index.js"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing Express&lt;/strong&gt;&lt;br&gt;
To use Express with Node we'll need to ensure we install Express, to do so, inside your root folder, open up your terminal - if you're on VSCode, simply guide your cursor to the top menu bar and click 'Terminal' &lt;br&gt;
Inside of your terminal:&lt;br&gt;
Run this command &lt;code&gt;% npm i express&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NkRNSsYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdcsg8z3ssx6j80xg4ks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NkRNSsYd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdcsg8z3ssx6j80xg4ks.png" alt="npm i express command" width="880" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to define express at the top of our index.js by typing: &lt;br&gt;
&lt;code&gt;const express = require('express')&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
and we'll set app&lt;br&gt;
&lt;code&gt;const app = express()&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Your file should look something like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gnF2Ht9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4y2au5rk13fmfcffrfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gnF2Ht9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4y2au5rk13fmfcffrfg.png" alt="Express route" width="880" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will facilitate our route and handle our simple get request. A GET request is an HTTP method. &lt;/p&gt;

&lt;p&gt;Now copy and paste or type the following into index.js:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.get('/', (req, res) =&amp;gt; {&lt;br&gt;
    res.send('Hello, World!')&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This code sets our "home" route, which in our case is just localhost:3000 and our express callback will take in two parameters &lt;code&gt;(req, res)&lt;/code&gt; which represents an HTTP request and response. We then send our message with &lt;code&gt;res.send('Hello, World!')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lastly, we will display a simple confirmation message to our console with the following code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.listen(3000, () =&amp;gt; {&lt;br&gt;
    console.log("Listening on port 3000")&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your index.js file should look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w8X2YPNf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj89ickn1qbcv96388eq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w8X2YPNf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj89ickn1qbcv96388eq.png" alt="code example" width="880" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we can now run our node server by typing this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;node index.js&lt;/code&gt; &lt;br&gt;
You should see this in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gSuOfXf---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl5d3wsad24frvstkv5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gSuOfXf---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl5d3wsad24frvstkv5f.png" alt="terminal" width="880" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if we head over to localhost:3000 you should see&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_jeNZo6Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynz4vjkj7umh3k9iyp24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_jeNZo6Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynz4vjkj7umh3k9iyp24.png" alt="localhost:3000 - Hello, World!" width="880" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! Congratulations, you have successful built your first route with Node and Express. Woohoo! 🎉 &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>node</category>
      <category>express</category>
    </item>
  </channel>
</rss>
