<?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: Ayodeji Akinborewa</title>
    <description>The latest articles on DEV Community by Ayodeji Akinborewa (@dejiborewa).</description>
    <link>https://dev.to/dejiborewa</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%2F876051%2F7c78590d-1347-40c2-a31e-433278351acd.jpeg</url>
      <title>DEV Community: Ayodeji Akinborewa</title>
      <link>https://dev.to/dejiborewa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dejiborewa"/>
    <language>en</language>
    <item>
      <title>Setting up &amp; serving static files in ExpressJS</title>
      <dc:creator>Ayodeji Akinborewa</dc:creator>
      <pubDate>Fri, 24 Jun 2022 15:43:06 +0000</pubDate>
      <link>https://dev.to/dejiborewa/setting-up-serving-static-files-in-expressjs-36f4</link>
      <guid>https://dev.to/dejiborewa/setting-up-serving-static-files-in-expressjs-36f4</guid>
      <description>&lt;p&gt;Setting up a server locally shouldn't be such a daunting task, and quite frankly it isn't.&lt;/p&gt;

&lt;p&gt;For starters, servers are computers, like the one you're probably reading this article with; except they usually don't have a graphical interface e.g monitor. Basically, they are just stacks of &lt;strong&gt;huge&lt;/strong&gt; processing units that execute high-volume operations.&lt;/p&gt;

&lt;p&gt;Like frontend applications, there is a myriad of frameworks used on the server-side (backend) to help foster development and maintain best practices.&lt;/p&gt;

&lt;h4&gt;
  
  
  So what is Express?
&lt;/h4&gt;

&lt;p&gt;Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Technically, nodeJS can be used to set up a server but we would not be using it because it requires more code and effort. Conversely, Express provides abstractions based on core node modules that we would leverage. &lt;/p&gt;

&lt;p&gt;So, say you want to serve a simple portfolio website with an Express server and your folder structure looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main
 - public
   - style.css
   - index.js
   - index.html
   - about.html
 - server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;If you don't have node and npm installed, follow this link for installation&lt;/em&gt; &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;cd&lt;/code&gt; into the &lt;code&gt;main&lt;/code&gt; folder, run &lt;code&gt;npm init --yes&lt;/code&gt; and &lt;code&gt;npm install express&lt;/code&gt;. I would also advice you install &lt;code&gt;nodemon&lt;/code&gt;, a package that recompiles your server code with every change you make to your code; Otherwise, you would have to stop and restart your server on every change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febabrc93sgfrit0trwuk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febabrc93sgfrit0trwuk.png" alt="npm install express"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If you running your server with &lt;code&gt;nodemon&lt;/code&gt;, ensure you update your &lt;code&gt;package.json&lt;/code&gt; scripts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d7t55w16xoxasx0pig8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d7t55w16xoxasx0pig8.png" alt="Updating your scripts in package.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;server.js&lt;/code&gt;, we import the main express module and assign it to a variable, then invoke the function and assign it to another variable &lt;code&gt;app&lt;/code&gt;. NodeJS uses its default CommonJS module system for importing and exporting modules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4krom8mwq3fvh4v7plek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4krom8mwq3fvh4v7plek.png" alt="create express server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check that terminal is currently reading the &lt;code&gt;main&lt;/code&gt; folder, run &lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;BOOM!!&lt;/em&gt;, your express server is up; you should see 'listening on port 4000' in your console. &lt;/p&gt;

&lt;p&gt;You might be thinking what the hell is this mysterious number?? Well, It is just an arbitrary port number, servers have ports that are specific to executing different protocols and if we were setting this up on a cloud resource, we wouldn't use an arbitrary number. But then again, that's beyond the scope of this article and since we are running our server locally any number works, feel free to mess around here.&lt;/p&gt;

&lt;p&gt;This is the point you open your browser and navigate to &lt;code&gt;localhost:4000&lt;/code&gt;. We currently have a blank page, which is normal; the server is running but we're not serving any files yet.&lt;/p&gt;

&lt;p&gt;We can try sending raw text back to our browser like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6n79bavfvyl79balmtz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6n79bavfvyl79balmtz.png" alt="res.send()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let me quickly break down our new line, Express calls a &lt;code&gt;GET&lt;/code&gt; method - remember HTTP verbs &lt;code&gt;(GET, POST, PUT, PATCH, DELETE)&lt;/code&gt;, you can call them in express as &lt;code&gt;app.get()&lt;/code&gt;, &lt;code&gt;app.post()&lt;/code&gt; etc, depending on what you're trying to do, here we are making a GET request. The first parameter in our GET method is our route name, and the second is a callback function with two objects - request and response &lt;code&gt;(req, res)&lt;/code&gt;; feel free to log these objects to see the different properties on them. &lt;code&gt;res.send()&lt;/code&gt; returns the response to the user's request, here we are sending plain text.&lt;/p&gt;

&lt;p&gt;Now refresh your browse: &lt;code&gt;Hello world&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To the fun part: As I mentioned earlier, we are going to use a built-in node module called &lt;code&gt;path&lt;/code&gt;; it allows us to manipulate directories and access files within our node environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yyyoqowiffct1nvouva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yyyoqowiffct1nvouva.png" alt="Using node path to read files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We import the &lt;code&gt;path&lt;/code&gt; module, &lt;code&gt;path&lt;/code&gt; has a &lt;code&gt;join&lt;/code&gt; method that is used to join a number of path-segments to form a single path. &lt;code&gt;__dirname&lt;/code&gt; returns the absolute path with respect to where it's executed, we then join the path returned from &lt;code&gt;__dirname&lt;/code&gt; with &lt;code&gt;public/index.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the panoptic ones, you would also notice we are calling a different method &lt;code&gt;res.sendFile()&lt;/code&gt;, this is a specific method for sending files over a response,  it also sets the &lt;code&gt;Content-Type&lt;/code&gt; response HTTP header field based on the filename extension.&lt;/p&gt;

&lt;p&gt;Now refresh your browser, you should see that your html file is rendered but your styles aren't loaded. Let's inspect with our browser dev tool, navigate to the network tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz7oleph9ppuvvj6f28f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz7oleph9ppuvvj6f28f.png" alt="Network tab, Chrome dev tools"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the &lt;code&gt;.css&lt;/code&gt; and &lt;code&gt;.js&lt;/code&gt; files returned a 404 error status code i.e the server wasn't able to find the resource requested. This is usually the case when file you're requesting for, is also trying to get another external resource like our &lt;code&gt;.css&lt;/code&gt; and &lt;code&gt;.js&lt;/code&gt;, which are imported in our &lt;code&gt;link&lt;/code&gt; and &lt;code&gt;script&lt;/code&gt; html tags respectively.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stressed??&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03nj61qbh2ksvgmvw088.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F03nj61qbh2ksvgmvw088.gif" alt="Calma calma"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The good guys at Express have designed and built a middleware that helps us handle this cases. A middleware is a piece of software that sits between our request and response to process data exchange and other application-specific functions.&lt;/p&gt;

&lt;p&gt;Middleware can be external modules or an abstracted piece of internal logic in our app. To &lt;strong&gt;use&lt;/strong&gt; them, Express provides a built in &lt;code&gt;use&lt;/code&gt; method - &lt;em&gt;apt right? I know.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo69lkpextw0h51sibbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo69lkpextw0h51sibbg.png" alt="using express.static to serve static files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;express.static()&lt;/code&gt; serves static files such as images, CSS files, and JavaScript files. The first parameter it accepts, is the folder containing the static assets. (Be mindful to call the middleware method before your routes).&lt;/p&gt;

&lt;p&gt;Let's create another route to &lt;code&gt;about.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfj0tajtzlujucb432jq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfj0tajtzlujucb432jq.png" alt="Multiple routes in expressJS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  SUIIIII
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F311pd93rheljtt7yldr2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F311pd93rheljtt7yldr2.gif" alt="SUIIIII"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we've expressJS serving our portfolio website. &lt;/p&gt;

&lt;p&gt;Setting up multiple static files and different subdirectories in your public folder won't break the server, as long as they are properly imported. Conceptually, Express will bundles all our files into the public folder.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bye for now&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Ayodeji Akinborewa.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>servers</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
