<?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: Murat Çorlu</title>
    <description>The latest articles on DEV Community by Murat Çorlu (@muratcorlu).</description>
    <link>https://dev.to/muratcorlu</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%2F183887%2F2f946b23-226f-41f5-acd0-b08bf1f0fd6e.jpeg</url>
      <title>DEV Community: Murat Çorlu</title>
      <link>https://dev.to/muratcorlu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muratcorlu"/>
    <language>en</language>
    <item>
      <title>Front-end may soon need new sub-disciplines</title>
      <dc:creator>Murat Çorlu</dc:creator>
      <pubDate>Sat, 23 Jul 2022 19:14:53 +0000</pubDate>
      <link>https://dev.to/muratcorlu/front-end-may-soon-need-new-sub-disciplines-6p2</link>
      <guid>https://dev.to/muratcorlu/front-end-may-soon-need-new-sub-disciplines-6p2</guid>
      <description>&lt;p&gt;Backend effort on most projects is declining every day with the help of high abstraction by cloud services. We can already make complete client-side applications with dynamic and collaborated data by just using some public services (like &lt;a href="https://firebase.google.com/"&gt;Firebase&lt;/a&gt; or &lt;a href="https://developer.apple.com/icloud/cloudkit/"&gt;CloudKit&lt;/a&gt;). We can manage &lt;a href="https://auth0.com/"&gt;authentication&lt;/a&gt; without having any server to maintain. Many authentication providers and even some authentication services allow us to do that.&lt;/p&gt;

&lt;p&gt;And obviously, things will not stop here. It’s not hard to imagine that we’ll have more database services where we can directly write/read data from our front-ends securely and efficiently. The backend work of most of the projects will be just connecting and configuring multiple cloud services to each other. &lt;/p&gt;

&lt;p&gt;So, will every web developer be a “front-end developer”? Of course, there will always be a need for more low-level server-side specialists, at least for creating those cloud services. But maybe 90% of the coding effort for web projects will be done on the client side. And this will mean the “front-end” will have more and more new responsibilities and challenges.&lt;/p&gt;

&lt;p&gt;We can already see those with the new web APIs every day we start to hear, like Web Assembly, Web Worker, Web GPU, etc. The work we’ll begin to handle those new layers will be not just “basic UI” things. Instead, we’ll move some new stuff to the “front-end” that we previously handled on our backends with the help of the new power we’ll have in browsers.&lt;/p&gt;

&lt;p&gt;Take Web Assembly, for example. Web Assembly allows us to run some libraries in the browser with native performance. You cannot (and intend to) do DOM manipulations inside the Web Assembly layer. Still, you can make very CPU-intensive things like image processing, data cryptography, or even maybe Machine Learning.&lt;/p&gt;

&lt;p&gt;Web Workers gives us to run some other JavaScript routines outside of our render cycles without blocking UI. WebGPU will give us the full power of GPU to make even more efficient graph calculations, crypto calculations, or 3D renderings. WebRTC allows it to create a peer-to-peer connection with another user of our client app and send/receive any data, including streaming video or audio. File System Access API gives full access to the local file system of the user to read/write files and directories. &lt;/p&gt;

&lt;p&gt;These are not very familiar things that we do daily as front-end developers. In the meantime, real UI work effort is not getting less. Creating durable, reusable, flexible, easy-to-use, and accessible components is still a big challenge. Providing the best UI with the best UX needs neat work with designers and product owners.&lt;/p&gt;

&lt;p&gt;As a result, I expect we’ll need to consider splitting those new responsibilities inside web client-side development. Let me name them “Web UI” and “Web Core.”&lt;/p&gt;

&lt;h2&gt;
  
  
  Web UI Developers
&lt;/h2&gt;

&lt;p&gt;Maybe we can roughly list the responsibilities of Web UI developers as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web Components&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;Forms&lt;/li&gt;
&lt;li&gt;Animations&lt;/li&gt;
&lt;li&gt;Accessibility&lt;/li&gt;
&lt;li&gt;Web Fonts&lt;/li&gt;
&lt;li&gt;Canvas&lt;/li&gt;
&lt;li&gt;SVG, SVG filters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the items that we are very familiar with as front-end developers. They all are directly related to UI and user interaction. And we all know that if you consider being a specialist in all of those areas and being able to create a very well-implemented web application, there will be many challenges waiting for us.&lt;/p&gt;

&lt;p&gt;This area is also where our popular web frameworks are responsible. We make components with Lit, manage complex forms with Angular, or create responsive grids with Tailwind. (The last one was a joke)&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Core Developers
&lt;/h2&gt;

&lt;p&gt;Web Core developers may be mainly responsible for the business logic and service-like backend part of the client app. These things will not directly change UI but primarily data management, business logic, and scalability (very familiar things for current backend developers, right?). If we want to list some technology names for Web Core developers, we may record:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WebGL - WebGPU&lt;/li&gt;
&lt;li&gt;Worker&lt;/li&gt;
&lt;li&gt;Web Assembly&lt;/li&gt;
&lt;li&gt;Web Crypto&lt;/li&gt;
&lt;li&gt;IndexedDB&lt;/li&gt;
&lt;li&gt;File System Access API&lt;/li&gt;
&lt;li&gt;WebRTC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no React or Angular here. Nothing here for them. But for complex web applications, those APIs are like treasures. Also, asking WebGPU or WebAssembly expertise from a senior React developer will not be fair.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example project team structure
&lt;/h2&gt;

&lt;p&gt;Let’s consider a project as a photo/video management web app. We’ll make a web application to add our photos and videos to a library, and edit, categorize or share them.&lt;/p&gt;

&lt;p&gt;We’ll not have a backend code for this. We’ll use a database and a file storage service, which can work with an authentication service. Those services will be fully-managed, external services. We’ll handle image/video editing entirely on the client site. And we’ll store all data on our storage services in an e2e encrypted way.&lt;/p&gt;

&lt;p&gt;As you can see, there are pretty many things to do for both UI and “Core” sides in this project. For UI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A fancy UI to list, search and add many photos and videos to the library.&lt;/li&gt;
&lt;li&gt;A fully-featured and easy-to-use UI for editing images and videos&lt;/li&gt;
&lt;li&gt;Share albums with others, comment on photos&lt;/li&gt;
&lt;li&gt;Making this app “accessible” as much as possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other side, there are also huge challenges on the core side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing some “Web Workers” to communicate with our cloud services in a non-blocking manner&lt;/li&gt;
&lt;li&gt;Writing a very efficient image manipulation library&lt;/li&gt;
&lt;li&gt;Writing a very efficient video editing library&lt;/li&gt;
&lt;li&gt;An API to crypt/decrypt images and videos before sending them to our storage service and after fetching them&lt;/li&gt;
&lt;li&gt;Storing some part of the data locally to make our app run offline and sync with services later&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we list our tasks like this, I think separating the types of tasks becomes very clear. “Web Core” tasks are pure IO challenges that never touch DOM or CSS. On the other hand, the APIs that the “Web Core” team will provide to the “Web UI” team will be very similar to what we currently have with the “Frontend - Backend” world. JS interfaces replace REST or GraphQL APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Titles and disciplines are not big walls to keep developers in their responsibilities forever. Instead, they are helpers focusing on one thing at a time. Distraction is one of the biggest diseases of our time. And I think it’s very valuable for every IT professional to focus on quality, not quantity.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Mocking REST endpoints in web apps, the easy way</title>
      <dc:creator>Murat Çorlu</dc:creator>
      <pubDate>Wed, 01 Sep 2021 14:03:31 +0000</pubDate>
      <link>https://dev.to/muratcorlu/mocking-rest-endpoints-in-web-apps-the-easy-way-3b8m</link>
      <guid>https://dev.to/muratcorlu/mocking-rest-endpoints-in-web-apps-the-easy-way-3b8m</guid>
      <description>&lt;p&gt;tldr; Use client-side mocks for your REST API endpoints with &lt;a href="https://github.com/muratcorlu/connect-api-mocker" rel="noopener noreferrer"&gt;connect-api-mocker&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we need mocks?
&lt;/h2&gt;

&lt;p&gt;At one of my previous companies, in the early times, we had a flow like that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A feature implementation is created by design team&lt;/li&gt;
&lt;li&gt;Designs come to front-end team(us). We create static HTML pages, then task goes to back-end team.&lt;/li&gt;
&lt;li&gt;Back-end team makes HTML dynamic.&lt;/li&gt;
&lt;li&gt;Then probably because of we need some re-work and some extra JavaScript codes, the task returns back to front-end team.&lt;/li&gt;
&lt;li&gt;Then task goes testing team…&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After we changed our architecture as a Single Page Application and started to use REST API as the back-end, we wanted to make that flow faster and decided to use client side mocking solutions. The idea was to change our flow like that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Design comes to front-end team&lt;/li&gt;
&lt;li&gt;Front-end and back-end team decides endpoint structures that they will need for that task.&lt;/li&gt;
&lt;li&gt;Front-end team uses mocks and starts implementation without waiting for real endpoint&lt;/li&gt;
&lt;li&gt;Back-end team tries to create that endpoint. When endpoint is ready, front-end team doesn’t need any change for release.
That means, you can develop your task by parallel with your back-end team. This will be improve your productivity and you will not have lot’s of troubles about your back-end dependencies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This would be also good for reproducing interesting bugs. Sometimes we had some very exceptional but also annoying issues that are very hard to reproduce with real data. With an opportunity like defining API responses in a simple way, it would be very simple to create some awkward situations for testing.&lt;/p&gt;

&lt;p&gt;Another advantage would have a development environment that doesn’t really need to connect some remote servers, even internet. You would not need a working REST API server to work.&lt;/p&gt;

&lt;p&gt;Then we created connect-api-mocker&lt;/p&gt;

&lt;h2&gt;
  
  
  What is connect-api-mocker?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/muratcorlu/connect-api-mocker" rel="noopener noreferrer"&gt;connect-api-mocker&lt;/a&gt; is an npm package (actually a simple connect middleware) that can be used for creating fake responses for REST endpoints.&lt;/p&gt;

&lt;p&gt;We created the first version in 2013 at one of my previous companies and also shared on Github. After I left there, I forked the repository and continued to improve it to use it on my clients’ projects. I also created an &lt;a href="https://www.npmjs.com/package/connect-api-mocker" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; and added many features.&lt;/p&gt;

&lt;p&gt;connect-api-mocker, uses some mock json files in filesystem that are created with same folder structure with REST endpoints. This makes it very simple to use and modify it. But it’s not just that, there are more!&lt;/p&gt;

&lt;h2&gt;
  
  
  How is it easy to use mocking
&lt;/h2&gt;

&lt;p&gt;You can use connect-api-mocker with Webpack, Grunt, Gulp and other connect.js powered environments. You can install connect-api-mocker with 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 i --save-dev connect-api-mocker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To use it with a webpack project, you need to use &lt;code&gt;setup&lt;/code&gt; property of &lt;code&gt;webpack-dev-server&lt;/code&gt;:&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;var&lt;/span&gt; &lt;span class="nx"&gt;apiMocker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;mocker&lt;/span&gt;&lt;span class="err"&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;setup&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;app&lt;/span&gt;&lt;span class="p"&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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;apiMocker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mocks/api&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="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That configuration means, api mocker will try to respond requests that has url starts with &lt;code&gt;/api&lt;/code&gt; by some files in the &lt;code&gt;mocks/api&lt;/code&gt; folder. For example, if you make a &lt;code&gt;GET&lt;/code&gt; request to &lt;code&gt;/api/users/1&lt;/code&gt; it will respond that request with file located in &lt;code&gt;mocks/api/users/1/GET.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You already got the idea, right? The request mapping structure is like that:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[METHOD] /{baseUrl}/{path} &amp;gt; {basePath}/{path}/[METHOD].json&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It will work all kind of request methods like &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, &lt;code&gt;OPTIONS&lt;/code&gt; etc. Some examples:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;POST /api/users/1 ==&amp;gt; mocks/api/users/1/POST.json&lt;/code&gt;&lt;br&gt;
&lt;code&gt;DELETE /api/users/1 ==&amp;gt; mocks/api/users/1/DELETE.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JSON files are normal, simple JSON files.&lt;/p&gt;
&lt;h2&gt;
  
  
  Custom responses
&lt;/h2&gt;

&lt;p&gt;Creating mock json files with same path by request path is a good idea, but it’s not enough for all cases. If you also want to create some different mock responses for query or other request parameters, you will need more. This is where custom response support comes in.&lt;/p&gt;

&lt;p&gt;Custom responses are simple connect middleware functions that also created in same path except(and of course) with a JS file extension. For example, instead of using simple &lt;code&gt;POST.json&lt;/code&gt; file, you can create a &lt;code&gt;POST.js&lt;/code&gt; file with content like that:&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;X-Auth-Key&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="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&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="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST.json&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="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;__dirname&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;If you place that file to &lt;code&gt;mocks/api/users/POST.js&lt;/code&gt; and if you make a &lt;code&gt;POST&lt;/code&gt; request to &lt;code&gt;/api/users&lt;/code&gt;, api-mocker will run your custom response middleware instead of returning &lt;code&gt;POST.json&lt;/code&gt; file in the path. In the custom response example above, we are testing existence of a header and if it’s not there we are responding with a 403 error, if it’s there, we are simply sending POST.json file content in the same folder.&lt;br&gt;
Can you see the power of that functions?&lt;/p&gt;

&lt;p&gt;Another example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET /api/users?type=active ==&amp;gt; mocks/api/users/GET.js&lt;/code&gt;&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;targetFileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Check is a type parameter exist&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;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Generate a new targetfilename with that type parameter&lt;/span&gt;
    &lt;span class="nx"&gt;targetFileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&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;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.json&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 file does not exist then respond with 404 header&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accessSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;targetFileName&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&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;span class="c1"&gt;// Respond with targetFileName&lt;/span&gt;
  &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;targetFileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;__dirname&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;With an example like above, we can send separate JSON responses depending on a query parameter.&lt;/p&gt;

&lt;p&gt;And another example with another good opportunity:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET /api/gateway/3213132 ==&amp;gt; mocks/api/gateway/3213132/GET.js&lt;/code&gt;&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;var&lt;/span&gt; &lt;span class="nx"&gt;requestCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;requestCount&lt;/span&gt;&lt;span class="o"&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;requestCount&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="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notready&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="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="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ready&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Because of connect servers are running as an instance, we can store some in-memory variables like the example above. In that example we are responding different result depending of request count to that endpoint.&lt;/p&gt;

&lt;p&gt;As you can see, you don’t have any limit with custom responses!&lt;/p&gt;
&lt;h2&gt;
  
  
  Wildcard support
&lt;/h2&gt;

&lt;p&gt;We need one more thing: defining some wildcards in path definitions. We need that because it’s not very cool to create folders for every ids of our models like &lt;code&gt;users/1&lt;/code&gt;, &lt;code&gt;users/2&lt;/code&gt;, &lt;code&gt;users/3&lt;/code&gt; etc. What is about to create o folder like &lt;code&gt;users/__user_id__&lt;/code&gt; and use that &lt;code&gt;user_id&lt;/code&gt; variable in our custom responses as a request parameter? Yeah, that would be great! The good news is it’s already there!&lt;/p&gt;

&lt;p&gt;So if you make a request something like that:&lt;br&gt;
&lt;code&gt;GET /api/projects/50b3c102–298a-45ad-acad-e21b6c1bbdcc/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can define a mock in &lt;code&gt;mocks/api/projects/50b3c102-298a-45ad-acad-e21b6c1bbdcc/GET.json&lt;/code&gt; If api mocker finds a file with that path, it will respond with that. But another option is creating a file in a path like &lt;code&gt;mocks/api/projects/__project_uuid__/GET.json&lt;/code&gt;. In that case, all requests to projects/{something} that has not a specific mock file, will be responded with same response. Also it’s possible to define a custom response in the same folder with a &lt;code&gt;GET.js&lt;/code&gt; file and produce a dynamic response depend on wildcard variable that defined in folder name( project_uuid ). For example:&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;id&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;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;project_uuid&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;Wildcard definitions are starting and ending with double underscore. And it’s the last that api mocker checked. So looking for a mock response flow is like that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;look for &lt;code&gt;mocks/api/projects/50b3c102-298a-45ad-acad-e21b6c1bbdcc/GET.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;look for &lt;code&gt;mocks/api/projects/50b3c102-298a-45ad-acad-e21b6c1bbdcc/GET.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;look for &lt;code&gt;mocks/api/projects/__{something}__/GET.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;look for &lt;code&gt;mocks/api/projects/__{something}__/GET.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also you can define multiple nested wildcards for a path like: &lt;code&gt;mocks/api/__resource_type__/__resource_uuid__/GET.json&lt;/code&gt;. That will be respond all GET requests to &lt;code&gt;api/*/*&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  NextOnNotFound setting
&lt;/h2&gt;

&lt;p&gt;By default, if you enable api mocker for a path and you don’t define a mock file for a request, it will be respond with a 404 Not Found error. But it’s also available to set nextOnNotFound to true then all not found requests will be passed to next middleware. Sample configuration:&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;apiMocker&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mocks/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;nextOnNotFound&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is very powerful if you only want to mock some part of your endpoints instead of all of them. With the help of proxy features of Webpack, Grunt or Gulp, you can continue to get responses from your real api that you don’t want to mock. An example for webpack:&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="c1"&gt;// webpack.config.js&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="nx"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &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="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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/backend/api/v1/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;apiMocker&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;         &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mocks/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;nextOnNotFound&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/analytics/api/v1/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;apiMocker&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;         &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mocks/analytics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;nextOnNotFound&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="p"&gt;},&lt;/span&gt;
&lt;span class="nx"&gt;proxy&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;/backend&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="nl"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.backend.yourdomain.com&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="s1"&gt;/analytics&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="nl"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.analytics.yourdomain.com&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the example above, we defined 2 api mocker for 2 separate paths and also defined some proxies to real api servers for that paths. With the help of nextOnNotFound option, requests to that paths that doesn’t have a mock definition will be passed to that proxies.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I used connect-api-mocker nearly on all my projects for last 4–5 years with several teams and we were very happy for getting advantages of it. I also suggest to use it or some solutions like that in your projects to simplify and make fast your development environment. Because of it’s also open source, you are very welcome for your contributions.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/muratcorlu" rel="noopener noreferrer"&gt;
        muratcorlu
      &lt;/a&gt; / &lt;a href="https://github.com/muratcorlu/connect-api-mocker" rel="noopener noreferrer"&gt;
        connect-api-mocker
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Connect middleware that creates mocks for REST APIs
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;Do you have an idea or a problem with api mocker? Just create an issue on Github repository and let’s talk about it.&lt;/p&gt;

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