<?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: lucsan</title>
    <description>The latest articles on DEV Community by lucsan (@lucsan).</description>
    <link>https://dev.to/lucsan</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%2F179980%2F9ca5af26-374f-4339-9a32-03bd1513c890.jpeg</url>
      <title>DEV Community: lucsan</title>
      <link>https://dev.to/lucsan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucsan"/>
    <language>en</language>
    <item>
      <title>Need a simple project to sharpen your skills?</title>
      <dc:creator>lucsan</dc:creator>
      <pubDate>Wed, 24 Jul 2019 12:28:09 +0000</pubDate>
      <link>https://dev.to/lucsan/need-a-simple-project-to-sharpen-your-skills-52h1</link>
      <guid>https://dev.to/lucsan/need-a-simple-project-to-sharpen-your-skills-52h1</guid>
      <description>&lt;p&gt;I often hear people saying they need a simple project but they are overwhelmed by all the options, well he's the simple guide to creating a simple project for your needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First you need a little bit of data&lt;/strong&gt;, this is the internet after all, and its all about the &lt;em&gt;infonation&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Don't 😰 sweat it, you don't want to spend ages searching the internet, finding heaps of data, parsing it, cleaning it, sorting it (unless your into Machine Learning), in stead make a simple table 📑.&lt;/p&gt;

&lt;p&gt;It doesn't matter if &lt;strong&gt;your data is a piddly little 4 by 4 table&lt;/strong&gt; of, say, fruit, price, weight, and img (with 4 fruits in it), this is sufficient data for any project, simply replace fruit with your favorite thing if you don't like fruit, you weirdo.&lt;/p&gt;

&lt;p&gt;So armed with a 🍌 banana and 3 other fruit (🍎🥑🐷), the next part, while simple, is harder, you need to focus on the part of the job you want to demonstrate.&lt;/p&gt;

&lt;p&gt;So, if you wish to demonstrate DB mastery, put the table in a database, for CSS magicians, just make the table in html then look at it in a browser, for programing gurus make the table an array or object or list, or directory, or data cluster.&lt;/p&gt;

&lt;p&gt;Now look at your table and 🤔 think about what next? what do you want to achieve? A page listing your fruit 📰 and an individual page for each fruit? a 📊 graph of fruitiness?&lt;/p&gt;

&lt;p&gt;For Database simply devise as many ways of getting your fruit data, for programming, as many was of manipulating your fruit data, and for front-end as many ways of presenting your fruit, as you can, then do a few of them.&lt;/p&gt;

&lt;h4&gt;
  
  
  🎈 At this point you have more or less completed your project, simple eh! 🎉
&lt;/h4&gt;

&lt;p&gt;If your a front-ender, then sensibly you made your table into a set of html pages with the data written directly into the pages. maybe you used Sass or Less, well you can upload it all to github and make a github page which will run your site.&lt;/p&gt;

&lt;p&gt;For the rest of us the matter of tool chain becomes more important, your luverly database with its tables and SQL statements that provide fruity 🍏 goodness in a variety of formats, can only be seen via a database viewer (ie: client), you probably want to display your 🍒 fruitformation on the webs as well. Similar may be said for programmers.&lt;/p&gt;

&lt;p&gt;It depends on how much of the development sequence you want to demonstrate as all parts can be dealt with by a &lt;em&gt;something else&lt;/em&gt;, to the extent you could create a project with bits found else where on the internet.&lt;/p&gt;

&lt;p&gt;Resources like Netlify (Amazon), Firebase (Google), and Glitch (don't know) provide webspace and tool chain. With Netlify you can use AWS lambda functions, and with Firebase the Google equivalent, and of course, you can use all three together in some way (ie: get Glitch to call an AWS lambda function and use Firebase user login).&lt;/p&gt;

&lt;p&gt;It may be that you can simply drag and drop your project into any of these. Alternately you can use your favorite database to html program which automatically generates web pages based on the database structure. Simply put, you can find a tool to do the jobs you don't want to. &lt;em&gt;(proviso: this rule is only good for small projects, and you may spend a lot of time learning new tools)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;💻 Here's an example of a small project I recently put together, with a simple stack.&lt;/p&gt;

&lt;p&gt;I chose Netlify (over firebase) for no reason xept I needed one or other. Code in github (obvo) , simple taskrunner in npm (using node scripts).&lt;/p&gt;

&lt;p&gt;If your interested &lt;a href="https://github.com/lucsan/SpockScissorsPaper"&gt;https://github.com/lucsan/SpockScissorsPaper&lt;/a&gt; for the engine and &lt;a href="https://spockpaperscissors.netlify.com/"&gt;https://spockpaperscissors.netlify.com/&lt;/a&gt; to see the web app. The code for the app is at  (rspalator running in React &lt;a href="https://github.com/lucsan/ReactScissorsPaper"&gt;https://github.com/lucsan/ReactScissorsPaper&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;For my little bit of data I remembered a project I devised a few years back a &lt;strong&gt;🎲 Rock Paper Scissors&lt;/strong&gt; engine which is given a set of items, and returns the result of pitching 2 items against each other. It takes any number of items, but defaults to 7.&lt;/p&gt;

&lt;p&gt;This project alone is quite nice from a TTD perspective and might replace fizz-buzz (5s n 7s) for variety, ironically I haven't implemented tests as I wanted the engine for the data. This also allows me to have a dependency (the RPS engine), thus creating a build chain.&lt;/p&gt;

&lt;p&gt;Then I created a React project which uses the RPS engine, in other words, a website which lets you play RPS.&lt;/p&gt;

&lt;p&gt;Code bases on github. Netlify configured to run node build scripts on master update, (and, as and when, I'll get Travis involved too). If you wanted to add more backend then perhaps add a login (or an old school high score board, with just the 3 letters) saved to some db or other (or file).&lt;/p&gt;

&lt;p&gt;For front-end there is plenty in the mechanics and the display of the game.  I added some base bootstrap files, but you cold go mad with SASS, Grunt, etc.&lt;/p&gt;

&lt;p&gt;Again the trick here is the simplicity of the data you use, and where you want to demonstrate skills, for Dev Ops just have lots of things which need to be connected, for backend make sure you add in a database, and maybe a shopping basket, for middle ware add in customer forms, room bookings etc, for front-end make sure your 3 pages look abso spanking.&lt;/p&gt;

&lt;p&gt;The real trick is to focus on the part (skill) you want to demonstrate, or just go organically, I have a little table what do I do with it? I know I'll present it well, then I know, I'll add customers, a shopping basket, tax, discounts, discounts on bulk, unavailable goods notification, email updates on the state of the fruits, a database for customer queries about fruit, and their availability, tests for whatever, a build chain to put it all together, in short, whatevs you need/want/are excited to do with your little bit of data.&lt;/p&gt;

&lt;p&gt;Hope this in some simple way helps you clarify your thoughts on your next project.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🛠 Bulding a node web server in 50 lines of code</title>
      <dc:creator>lucsan</dc:creator>
      <pubDate>Fri, 28 Jun 2019 15:06:21 +0000</pubDate>
      <link>https://dev.to/lucsan/bulding-a-node-web-server-in-50-lines-of-code-4hob</link>
      <guid>https://dev.to/lucsan/bulding-a-node-web-server-in-50-lines-of-code-4hob</guid>
      <description>&lt;p&gt;SimpServ simple Server is a, small footprint, node js, web Server, in a single page of javascript, which is exciting.&lt;/p&gt;

&lt;p&gt;The code is available at: &lt;a href="https://github.com/lucsan/simpserv/blob/master/simpserv.js"&gt;https://github.com/lucsan/simpserv/blob/master/simpserv.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can just copy the file to a 🍰 desired location, or if you like, do the whole npm git thing and 👽 clone or 🍴 fork, or 🥄 spoon from the remote repo &lt;a href="https://github.com/lucsan/simpserv"&gt;https://github.com/lucsan/simpserv&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;It's purpose is to provide a 💨quick and 💩dirty local dev server for js, html and css.&lt;/p&gt;

&lt;p&gt;For this 📃recipe we will require some node packages from the basic nodejs library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;http&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;http&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;url&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;url&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;fs&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;fs&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;Thus you will need &lt;strong&gt;&lt;em&gt;nodejs&lt;/em&gt;&lt;/strong&gt; installed on your machine. I will let you have the fun of working out 😱 how to do that.&lt;/p&gt;

&lt;p&gt;There is simple configuration in the file,&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;8080&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// And any port you like.&lt;/span&gt;
  &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// If you want to server files from a different location to where you started node.&lt;/span&gt;
  &lt;span class="na"&gt;autoload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Auto loads a window in the default browser.&lt;/span&gt;
  &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// You need an index.html in your root.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The 🚂'engine' is started when &lt;strong&gt;&lt;em&gt;start()&lt;/em&gt;&lt;/strong&gt; is called via the command line using node or npm.&lt;/p&gt;

&lt;h5&gt;
  
  
  📄For example:
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;node simpserv.js&lt;/code&gt;, or (if you have the package.json) &lt;code&gt;npm run serv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The start function is quite simple. A server (&lt;code&gt;http.createServer().listen()&lt;/code&gt;) is created and told to listen intently to the browser.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;onRequest()&lt;/code&gt; function is gently injected into &lt;code&gt;createServer&lt;/code&gt; making the &lt;code&gt;request.on&lt;/code&gt; event handler available, which waits eagerly to receive input (ie: a uri) from its loving browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onRequest&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;config&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="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;SimpServ running&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;err &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every time the &lt;strong&gt;&lt;em&gt;request.on&lt;/em&gt;&lt;/strong&gt; event receives a new uri it eagerly passes it to the router function.&lt;/p&gt;

&lt;p&gt;The router function is so super-duper it handles everything else. In a larger project this might be included as a module, anyway, here it is ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;
  &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&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="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;filename&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;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&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;span class="nx"&gt;filename&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;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;bits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/html;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;ct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/css;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;ct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/javascript;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&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;span class="nx"&gt;ct&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;charset=utf-8`&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the first few lines we extract essence of path from &lt;em&gt;root uri&lt;/em&gt;,&lt;/p&gt;

&lt;p&gt;ie: root = / or '', anything after the &lt;strong&gt;&lt;em&gt;/&lt;/em&gt;&lt;/strong&gt; is considered to be path.&lt;/p&gt;

&lt;p&gt;eg: &lt;br&gt;&lt;a href="https://localhost/myproject/"&gt;https://localhost/myproject/&lt;/a&gt; = / or ''&lt;br&gt;
&lt;br&gt;&lt;a href="https://localhost/myproject/index.html"&gt;https://localhost/myproject/index.html&lt;/a&gt; = index.html&lt;br&gt;
&lt;br&gt;&lt;a href="https://localhost/myproject/some/place"&gt;https://localhost/myproject/some/place&lt;/a&gt; = some/place&lt;/p&gt;

&lt;p&gt;We use index.html as the default file if the path is empty, but you could make this any file you like.&lt;/p&gt;

&lt;p&gt;Next we inspect the path for extension types. In this case we only care about css and js, which is not so very caring of us at all, but, other extensions can be catered for (eg: php).&lt;br&gt;
The desired MIME type is inserted into &lt;strong&gt;&lt;em&gt;ct&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally &lt;strong&gt;readfile&lt;/strong&gt; is called, passing in the path, and it serves the referenced file (which should be in, &lt;em&gt;or relative&lt;/em&gt;, to the root or &lt;em&gt;node running&lt;/em&gt; folder), if it can find it, else, errors out. 🤖 does not compute.&lt;/p&gt;

&lt;p&gt;This bit of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;platform&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;win32&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;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;child_process&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;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`start http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;config&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Opens a page in the default browser pointing at &lt;strong&gt;/&lt;/strong&gt; or &lt;strong&gt;/index.htm&lt;/strong&gt;, it only works on Windows, but you can find the equivalent for other OS's. This is a nice touch, though technically not part of the simple server as such.&lt;/p&gt;

&lt;p&gt;As you can see you could easy-peasy begin to modify this code for nefarious purposes (php, data endpoint), or to make it more comprehensive (and complex), or adapt it to a specific purpose (much as 🖌illustrated by the existing code).&lt;/p&gt;

&lt;p&gt;Here is a code snippet to respond to an API endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/customer&lt;/span&gt;&lt;span class="dl"&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/name&lt;/span&gt;&lt;span class="dl"&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;filepath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/customerName.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/address&lt;/span&gt;&lt;span class="dl"&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;filepath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/customerAddress.html&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;and is inserted here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (ext == 'js') ct = 'text/javascript;'
  // --- Insert filepath adapters here --- //
fs.readFile(path,  (err, page) =&amp;gt; { ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now when you visit &lt;strong&gt;/customer/name&lt;/strong&gt; the file customerName.html is returned, but this could just as easily be some json data, or &lt;strong&gt;filepath&lt;/strong&gt; could resolve to html returned from a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;filepath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myDataHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getSome&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;🏄‍Surfs up Dudes🌊&lt;/p&gt;

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