<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Binith Jayasinghe on Medium]]></title>
        <description><![CDATA[Stories by Binith Jayasinghe on Medium]]></description>
        <link>https://medium.com/@jpbinith?source=rss-fdc0ed0d3401------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*odPXk7jZRyPYvxMU.jpg</url>
            <title>Stories by Binith Jayasinghe on Medium</title>
            <link>https://medium.com/@jpbinith?source=rss-fdc0ed0d3401------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:11:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jpbinith/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I’m Trying to Build an AI That Reads the News Before Making Investment Decisions]]></title>
            <link>https://medium.com/@jpbinith/im-trying-to-build-an-ai-that-reads-the-news-before-making-investment-decisions-99a643a6c1e9?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/99a643a6c1e9</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[ai-architecture]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Sun, 19 Apr 2026 23:29:23 GMT</pubDate>
            <atom:updated>2026-04-20T02:35:41.660Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9nNl_0clPZp3mvLit7qfXA.png" /></figure><p>AI can answer almost anything. But when it comes to something like investing, there is a gap.</p><p>Most AI systems don’t actually know what’s happening right now. If you ask about gold prices or a stock, you’ll often get a confident answer based on general knowledge, not current events.</p><p>That got me thinking:</p><p>What if I build an AI system that reads the latest news first, and then makes a decision?</p><p>I’ve been working on this as a pet project. It’s still in the early stages, but it has already turned into a much deeper exploration of AI systems than I initially expected.</p><h3>The Idea</h3><p>The goal is simple in theory:</p><ol><li>Take a query like “What’s happening with gold prices right now?”</li><li>Fetch the latest news</li><li>Analyze the articles</li><li>Extract signals such as bullish and bearish indicators</li><li>Generate a structured, balanced view</li></ol><p>The key difference is that the answer should be grounded in real, recent information, not just generated from prior knowledge.</p><h3>What I’ve Built So Far</h3><p>The system currently has a few core components:</p><ul><li>A web scraping layer to collect news</li><li>A custom MCP (Model Context Protocol) server exposing tools</li><li>A local LLM (Llama 3 via Ollama)</li><li>A custom client application that orchestrates everything</li></ul><p>At a high level, the flow looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*rhXb77B_1KyMxghgc3zZKg.png" /></figure><p>This is very different from a typical chatbot. Instead of just prompting a model, I’m building a small system where the model can decide how to gather information before answering.</p><h3>How the System Decides What to Search</h3><p>One of the most interesting parts of this project is what happens after a user enters a query.</p><p>For example:</p><p>“What’s happening with gold prices right now?”</p><p>The system does not immediately fetch data. The LLM first interprets the intent.</p><p>It breaks the query down into something more actionable:</p><ul><li>“gold prices” indicates a financial market context</li><li>“right now” implies the need for real-time information</li><li>related signals might include inflation, interest rates, and central bank activity</li></ul><p>Instead of searching for a single keyword, the model effectively expands the problem into multiple relevant dimensions.</p><h3>Tool Selection</h3><p>Based on that understanding, the model decides which tools to use.</p><p>The MCP server exposes tools such as:</p><ul><li>search_market_news</li><li>get_ticker_news</li><li>get_article_content</li><li>summarize_market_news</li></ul><p>The model chooses which tool is appropriate for the task, rather than relying on hardcoded logic.</p><h3>Query Generation</h3><p>The model also reformulates the query into something more effective for retrieval. For example:</p><ul><li>“gold price outlook inflation interest rates”</li><li>“central bank gold demand trends”</li><li>“XAU macroeconomic drivers”</li></ul><p>So the system is not just searching. It is deciding what to search and how to search it.</p><h3>Tool Execution Loop</h3><p>Once a decision is made:</p><ol><li>The model calls a tool</li><li>The MCP server executes the request</li><li>Data is returned</li><li>The model continues reasoning with that data</li></ol><p>This creates a loop:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_n9_7pnCMoMNE7fKoybDXw.png" /></figure><p>This is a very different pattern from traditional applications.</p><h3>Building a Custom MCP Server</h3><p>To enable this behavior, I built a custom MCP server that exposes specific capabilities as tools.</p><p>MCP acts as a bridge between the LLM and external systems. It allows the model to call structured functions instead of relying purely on text generation.</p><h3>The Tools</h3><h4>search_market_news</h4><p>This is the main entry point.</p><p>It takes a query and returns a list of articles with:</p><ul><li>title</li><li>source</li><li>URL</li><li>timestamp (if available)</li></ul><p>Under the hood, it uses a web scraper to fetch and parse news pages.</p><h4>get_article_content</h4><p>This tool takes a URL and extracts the main content of the article.</p><p>This is important because headlines alone are not enough to understand context. Access to the article body allows for more grounded analysis.</p><h4>get_ticker_news</h4><p>This tool handles company-specific queries.</p><p>It takes a ticker and expands it into a more meaningful search query. For example:</p><p>NVDA becomes something like “Nvidia stock earnings AI demand”.</p><p>This improves relevance without requiring the user to craft perfect queries.</p><h4>summarize_market_news</h4><p>This is a helper tool that prepares structured summaries from raw articles. At the moment, most of the analysis still happens in the LLM, but this tool sets the foundation for more structured processing later.</p><h3>Web Scraping with Scrapling</h3><p>For data collection, I’m using Scrapling.</p><p>It provides a simple way to:</p><ul><li>fetch web pages</li><li>extract content using CSS selectors</li><li>convert unstructured HTML into structured data</li></ul><p>This works well for prototyping and gives me full control over the data pipeline.</p><h3>The Reality of Scraping</h3><p>One of the first issues I ran into was that everything appeared to work, but the system returned empty results.</p><p>The problem turned out to be broken selectors due to changes in page structure.</p><p>This highlighted an important point:</p><p>Scraping is one of the most fragile parts of the system. Small changes in HTML can silently break the pipeline.</p><p>To deal with this, I had to:</p><ul><li>inspect raw HTML responses</li><li>improve selector robustness</li><li>add better logging</li></ul><h3>Challenges I’ve Faced</h3><p>Even at this early stage, several challenges became clear.</p><h4>Tool Calling Is Not Reliable</h4><p>The model often understands that it should use a tool, but instead of actually calling it, it describes what it plans to do.</p><p>This means no tool execution happens, and the system fails to retrieve data.</p><p>This required more careful prompt design and stricter instructions.</p><h4>Replacing a Managed LLM</h4><p>In earlier experiments, I used a hosted model that handled tool orchestration automatically.</p><p>After switching to a local LLM, I had to:</p><ul><li>build a custom MCP client</li><li>manage tool calls manually</li><li>handle retries and edge cases</li></ul><p>This significantly increased complexity.</p><h4>Data Pipeline Fragility</h4><p>The system depends heavily on the quality of the scraped data.</p><p>If the data is incomplete or inconsistent, the model’s output quality drops immediately.</p><h3>What I’ve Learned</h3><p>This project shifted my perspective on AI systems.</p><ul><li>AI engineering is more about system design than model selection</li><li>Tool usage is not deterministic and requires guidance</li><li>Local LLMs require stronger prompts and constraints</li><li>Data quality has a direct impact on output quality</li><li>Observability and logging are critical for debugging</li></ul><h3>What’s Next</h3><p>There are several areas I want to improve.</p><h4>Improving Tool Reliability</h4><ul><li>enforce stricter tool-calling behavior</li><li>add retry mechanisms</li><li>introduce fallback strategies</li></ul><h4>Improving the Data Layer</h4><ul><li>move from scraping to more stable APIs</li><li>filter high-quality sources</li><li>remove duplicate or irrelevant articles</li></ul><h4>Using Full Article Content</h4><p>Currently, the system relies mostly on headlines and metadata.</p><p>The next step is to:</p><ul><li>fetch full article content</li><li>provide richer context to the model</li></ul><h4>Adding Signal Scoring</h4><p>Instead of just identifying bullish and bearish signals, I want to quantify them with scores and confidence levels.</p><h4>Moving to a Multi-Step Agent</h4><p>Rather than a single pass, the system can be broken into stages:</p><ol><li>Retrieve news</li><li>Extract key points</li><li>Identify signals</li><li>Generate final analysis</li></ol><h4>Building a Usable Interface</h4><p>Eventually, I would like to turn this into a small application where users can:</p><ul><li>search by ticker or topic</li><li>get structured summaries</li><li>view supporting articles</li></ul><h4>Adding Memory and Trends</h4><p>Storing past data would allow the system to:</p><ul><li>track trends over time</li><li>compare current sentiment with historical patterns</li></ul><h3>Final Thoughts</h3><p>This started as a simple idea:</p><p>Let AI read the news before answering.</p><p>But even in its current state, it has become clear that building systems like this is not just about prompts or models.</p><p>It is about connecting multiple components:</p><ul><li>data retrieval</li><li>tool orchestration</li><li>reasoning</li><li>and system design</li></ul><p>The project is still in its early stages, but it has already been a valuable learning experience.</p><p>The most interesting part is not the final answer the AI produces.</p><p>It is how the system decides what it needs to know before answering at all.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99a643a6c1e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Model Context Protocol (MCP) Is a Turning Point for AI Applications]]></title>
            <link>https://medium.com/@jpbinith/why-model-context-protocol-mcp-is-a-turning-point-for-ai-applications-df508f580f7a?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/df508f580f7a</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[mcp-protocol]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai-applications]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Sat, 18 Apr 2026 01:11:36 GMT</pubDate>
            <atom:updated>2026-04-18T01:11:36.721Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MCnLwrA8xjF1X-MVcOb8tA.png" /></figure><p>Artificial Intelligence has made massive progress in recent years, especially with the rise of large language models (LLMs). These models can generate text, answer questions, and assist with a wide range of tasks. However, despite their capabilities, most AI systems today share a fundamental limitation:</p><p>They can <em>suggest</em> actions, but they cannot <em>perform</em> them.</p><p>This is where the Model Context Protocol (MCP) becomes important.</p><h3>The Limitation of Today’s AI Systems</h3><p>Most AI applications follow a simple pattern:</p><p><strong>Input → LLM → Output</strong></p><p>The output is usually text. Even when the response is highly intelligent, it remains passive. For example, an AI might say:</p><ul><li>“You should send an email to the recruiter.”</li><li>“You can apply for this job using your resume.”</li><li>“Here’s how to update your database.”</li></ul><p>But the AI itself does not execute these actions. It leaves the final step to the user.</p><p>This gap between <em>intelligence</em> and <em>execution</em> is one of the biggest bottlenecks in building real world AI systems.</p><h3>Enter Model Context Protocol (MCP)</h3><p>Model Context Protocol (MCP) is designed to bridge this gap.</p><p>At its core, MCP provides a standardized way for AI systems to:</p><ul><li>Access external tools</li><li>Interact with APIs</li><li>Retrieve real time data</li><li>Perform actions in the real world</li></ul><p>Instead of being limited to generating text, AI systems can now <strong>operate as action oriented agents</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VyV18HDLz4U24UylEeVtgg.png" /></figure><h3>From “Talking AI” to “Doing AI”</h3><p>With MCP, the role of AI fundamentally changes.</p><p><strong>Before MCP:</strong></p><ul><li>AI explains what to do</li><li>AI provides suggestions</li><li>AI generates content</li></ul><p><strong>With MCP:</strong></p><ul><li>AI selects tools</li><li>AI executes tasks</li><li>AI completes workflows</li></ul><p>For example, instead of saying:</p><blockquote><em>“You should apply for this job”</em></blockquote><p>An MCP enabled system can:</p><ul><li>Analyze the job description</li><li>Tailor your resume</li><li>Generate a cover letter</li><li>Submit the application</li></ul><p>This shift transforms AI from a passive assistant into an active system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SAFOUUHv_IF196Zq9eczew.png" /></figure><h3>How MCP Works (Conceptually)</h3><p>MCP introduces a modular architecture where different components work together:</p><ul><li><strong>Host</strong> — The main AI application (e.g., chatbot, assistant)</li><li><strong>Client</strong> — The layer that connects the AI to external capabilities</li><li><strong>Server</strong> — A provider of tools, data, or services</li></ul><p>The workflow looks like this:</p><ol><li>The AI is aware of available tools</li><li>It decides which tool is relevant</li><li>It invokes the tool</li><li>It receives the result</li><li>It continues reasoning or completes the task</li></ol><p>This creates a loop where AI can <strong>think and act</strong>, not just respond.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*ho2B5EFmEjOWdKfaCRSTJg@2x.jpeg" /></figure><h3>The Power of Pluggable AI</h3><p>One of the most important ideas behind MCP is <strong>pluggability</strong>.</p><p>Instead of hardcoding every capability into the system, you expose tools as independent services. The AI can then:</p><ul><li>Discover available tools</li><li>Decide when to use them</li><li>Combine multiple tools to solve complex problems</li></ul><p>This makes AI systems:</p><ul><li><strong>Modular</strong> — Easy to extend with new capabilities</li><li><strong>Scalable</strong> — Works across multiple domains</li><li><strong>Maintainable</strong> — Tools can evolve independently</li></ul><p>It’s similar to how modern software systems use APIs, but now designed specifically for AI.</p><h3>Why MCP Matters for Real World Applications</h3><p>MCP is not just a technical improvement. It changes how we build AI products.</p><p>It enables:</p><ul><li>Workflow automation</li><li>Enterprise integrations</li><li>Real time decision systems</li><li>End to end task execution</li></ul><p>Instead of building isolated AI features, developers can now create <strong>complete AI driven systems</strong> that interact with real environments.</p><h3>A Simple Analogy</h3><p>Without MCP:</p><blockquote><em>AI is like a highly intelligent brain locked in a room.</em></blockquote><p>With MCP:</p><blockquote><em>AI becomes a brain connected to tools, systems, and the outside world.</em></blockquote><h3>Practical Impact</h3><p>For developers and engineers, MCP opens up new possibilities:</p><ul><li>Building AI systems that automate job applications</li><li>Creating assistants that interact with internal company tools</li><li>Developing AI driven dashboards that take actions, not just display insights</li><li>Designing end to end intelligent workflows</li></ul><p>This is especially powerful when combined with approaches like Retrieval Augmented Generation (RAG), where AI not only retrieves knowledge but also acts on it.</p><h3>The Bigger Shift: Toward Agentic AI</h3><p>The emergence of MCP signals a larger transition in AI:</p><p>From:</p><ul><li>Prompt based interactions</li><li>Static responses</li></ul><p>To:</p><ul><li>Autonomous agents</li><li>Dynamic workflows</li><li>Action driven systems</li></ul><p>This is often referred to as <strong>agentic AI</strong> systems that can plan, decide, and execute.</p><p>MCP provides a foundational layer for making this practical and scalable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*CJry4oTQyWt559Cv1ItXsw@2x.jpeg" /></figure><h3>Final Thoughts</h3><p>Model Context Protocol is not just another feature or framework. It represents a shift in how AI systems are designed and used.</p><p>By enabling AI to interact with tools and perform actions, MCP transforms LLMs from <strong>text generators</strong> into <strong>capable, real world systems</strong>.</p><p>As AI continues to evolve, approaches like MCP will play a critical role in moving from experimentation to production grade intelligence.</p><p>The future of AI is not just about generating better responses, it’s about building systems that can actually get things done.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df508f580f7a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is RAG Still Needed in the Era of Long Context LLMs?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@jpbinith/is-rag-still-needed-in-the-era-of-long-context-llms-da8e9ecd4f2d?source=rss-fdc0ed0d3401------2"><img src="https://cdn-images-1.medium.com/max/1536/1*yGFCBRSYzKX6OWgM8xuHGA.png" width="1536"></a></p><p class="medium-feed-snippet">With the rapid evolution of large language models (LLMs), especially those capable of handling extremely large context windows, a common&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@jpbinith/is-rag-still-needed-in-the-era-of-long-context-llms-da8e9ecd4f2d?source=rss-fdc0ed0d3401------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@jpbinith/is-rag-still-needed-in-the-era-of-long-context-llms-da8e9ecd4f2d?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/da8e9ecd4f2d</guid>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[generative-ai]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Thu, 16 Apr 2026 15:19:26 GMT</pubDate>
            <atom:updated>2026-04-16T15:19:26.450Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Why Do Front-End Frameworks Exist?]]></title>
            <link>https://medium.com/@jpbinith/why-do-front-end-frameworks-exist-9c344359f1ef?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c344359f1ef</guid>
            <category><![CDATA[client-side-rendering]]></category>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Wed, 20 Dec 2023 14:14:09 GMT</pubDate>
            <atom:updated>2023-12-20T14:14:09.982Z</atom:updated>
            <content:encoded><![CDATA[<p>Front-end frameworks like React have become essential in modern web development. But why do they exist, and why not just use Vanilla JavaScript? Let’s dive into the reasons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xCvo8oNYkY8NPgyf" /></figure><h4>The Evolution of Web Development</h4><p>Originally, websites were server-side rendered, with HTML, CSS, and JavaScript sent from the server to the browser. This approach was sufficient for the basic interactivity of early websites, where JavaScript was used for simple dynamics, often with the help of jQuery. This approach was sufficient for websites with limited dynamic content. Classic examples include WordPress sites, where JavaScript played a minor role, primarily for adding simple dynamic effects like animations or hover actions. However, as the web evolved, so did the complexity and functionality of websites, giving rise to single-page applications (SPAs).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*SMak89-WwcbmErJE7tM-Pw.png" /><figcaption>Server-side rendering</figcaption></figure><h4>The Rise of Client-Side Rendering</h4><p>Unlike traditional websites, single-page applications (SPAs) are rendered on the client side, feeling more like native applications with seamless interactions and no page reloads even when interacting with different elements. This shift led to a fundamental change in how web applications are built and managed. Despite their advantages, SPAs introduced complexities in managing data and the user interface (UI).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*XBvzZk6GOMm9OZr9cOPI4Q.png" /><figcaption>Client-side rendering</figcaption></figure><h4>The Challenges of Client-Side Rendering with Vanilla Javascript</h4><ol><li>Complex User Interfaces and Interactivity - Modern web applications involve complex user interfaces with interconnected data. Managing this complexity becomes increasingly challenging with just Vanilla JavaScript.</li><li>Direct DOM Manipulation - Building complex front-ends with Vanilla JavaScript often requires extensive DOM manipulation, which can lead to tangled, hard-to-maintain code.</li><li>State Management - In Vanilla JavaScript apps, the state is often stored directly in the DOM, leading to scattered, hard-to-track application states. This can result in bugs and maintenance challenges.</li><li>Keeping UI in Sync with Data - The core challenge in SPAs and modern web applications is maintaining synchronicity between the UI and the underlying data. This task, seemingly straightforward, is incredibly complex in practice. Consider a platform like Airbnb, where multiple pieces of data (apartment list, search bar, filters, etc.) must constantly interact and update in real time. Managing this interconnectivity using vanilla JavaScript can become overwhelmingly intricate and prone to bugs.</li></ol><h4>How Frameworks Address These Challenges</h4><ol><li>Synchronizing UI and Data - These frameworks handle the difficult task of keeping the user interface in sync with underlying data, allowing developers to focus on the data and UI design.</li><li>Structured Code and Best Practices - They enforce a structured way of writing code, reducing the chances of spaghetti code and improving maintainability.</li><li>Consistency in Development - Frameworks provide a unified approach to development, essential for teamwork and maintaining a consistent codebase.</li></ol><p>In summary, front-end frameworks exist to simplify the development of complex web applications. They abstract the challenges of state management and UI synchronization, enforce best coding practices, and provide a consistent structure for developers and teams.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c344359f1ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What I learned from my first Image Processing Project]]></title>
            <link>https://medium.com/analytics-vidhya/what-i-learned-from-my-first-image-processing-project-44433d1fddb7?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/44433d1fddb7</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[blood-vessel]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[segmentation]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Tue, 30 Jun 2020 21:46:55 GMT</pubDate>
            <atom:updated>2020-07-03T13:12:53.217Z</atom:updated>
            <content:encoded><![CDATA[<h4>Extract Blood Vessels from Fundus Image Using OpenCV Python</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*M0ROS1ctnErudPrmHpG-Dg.jpeg" /><figcaption>Fundus Image</figcaption></figure><p>Image processing is a method to perform some operations on an image, in order to get an enhanced image or to extract some useful information from it.</p><p>A pipeline is a some set of operations connected in a series, where the output of one is the input of the next one. In image processing, a task can be achieved through many different pipelines with different qualities.</p><p>My advise for anyone who is trying to start a project with a new framework is not to try to learn everything about that framework before the start of the project. You can get to know it while you are doing it. It’s better to have a general idea about what we can do with this technology and blueprint of the project. Lets take this project as a example.</p><p>In this project, our final goal is to extract large vessels and small vessels separately. The blood vessels were highlighted in the image because they were darker than others. So at first i thought if I am able to filter all the vessels by color(it does not work), then I can remove small vessels by smoothing or blurring and extract the large vessels. After that all I needed to do was subtract large vessels from all vessels and extract small vessels. This was my initial path to the goal.</p><p>Then I found a way to detect object using HSV color space. Here is the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00774bb3e6c12e09d1c50f979bf8b023/href">https://medium.com/media/00774bb3e6c12e09d1c50f979bf8b023/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z3ETv7pVB-tRljHaoAKGmA.png" /><figcaption>mask &amp; res output with trackbar</figcaption></figure><p>Here you can change parameters and see how the output changes. Click escape button to close it. The above figure was the best one I got by this and I was not satisfied with the outputs. Then I tried it with increasing brightness and contrast at the beginning. But it did not give me the thing I want. So I decided to move on to another method.</p><p>Then I found that <strong><em>Grayscale </em></strong>and<strong><em> green channel </em></strong>images are much easier to work within a variety of tasks like in many morphological operations and image segmentation problems and it is easier to distinguish features of an image when we deal with a single layered image. So I applied <strong><em>Grayscale</em></strong> and <strong><em>threshold</em></strong> with <strong><em>denoising</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*_QonSy6OVTCKsImsay_K6Q.jpeg" /><figcaption>Threshold &amp; denoising after applying grayscale</figcaption></figure><p>So we can clearly see that it had extracted more vessels than previous time but with lot of noises. So I had to find a way to remove those noises. Thanks to stack overflow I found a way to remove small objects but when I directly applied it to here, it removed some considerable amount of data. And again with the help of google &amp; stack overflow I found a way to tackle this problem with applying <strong><em>dilation</em></strong> and then removing small objects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b24b3a45a8b260cb766d52c03bdfaeff/href">https://medium.com/media/b24b3a45a8b260cb766d52c03bdfaeff/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bab3bdf96a68016c55ca14c3c80d846a/href">https://medium.com/media/bab3bdf96a68016c55ca14c3c80d846a/href</a></iframe><p>I tried dilation with different kernel sizes and end up with size (2, 2). Then I accidentally tried with the size (1, 2) and it had some extra details than kernel size (2, 2). Then I tried with the size (2, 1) and it also had some extra details than kernel size (2, 2) and even more details than (1, 2) while some were missing. When I merged both (1, 2) and (2, 1) images It gave a better output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*qWHieVXKgyhZJ6-zTmxk1w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*b_y7844WMKwdLGTSuaE8Nw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*eBgZUIXgxdIgGXBr2Wu73g.jpeg" /><figcaption>from the left ( with kernel size (2, 2), with kernel size (1, 2), with kernel size (2, 1) )</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*XTg9htGCtxivw-LrArP63Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*D9GAx4RhP1Wuhj3ns8ESog.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*Tcq-OpDAnHHn1BsKk6kxjQ.jpeg" /><figcaption>from the left ( after removing small objects with kernel size (2, 2), with kernel size (1, 2), with kernel size (2, 1) )</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*TcOPKnB1pIm87xm1u9axbQ.jpeg" /><figcaption>after merge images filtered with kernel size (1, 2) and (2, 1)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb006098468e7813f3c9eee80ca19ea9/href">https://medium.com/media/bb006098468e7813f3c9eee80ca19ea9/href</a></iframe><p>There may be some other pipelines which give a way better output than this. But I’m happy with what I got because this is my first image processing project and with the time I had 😉. You may have a better view, if we remove the background. You can done it using <strong>masking</strong> and <strong><em>bitwise_not</em></strong> operator.</p><p>A <strong>mask</strong> is a binary image consisting of zero- and non-zero values. If a <strong>mask</strong> is applied to another binary or to a <strong>grayscale</strong> image of the same size, all pixels which are zero in the <strong>mask</strong> are set to zero in the output image. All others remain unchanged.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d65423612ebc07f46c632a78db9458e/href">https://medium.com/media/9d65423612ebc07f46c632a78db9458e/href</a></iframe><p>In our case we can define a function like following to remove the background.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4859bbdad2ecd1b43bc1a1e994d2f50/href">https://medium.com/media/e4859bbdad2ecd1b43bc1a1e994d2f50/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*1zFZytQK8MSxPhtb4i2shQ.jpeg" /><figcaption>after removing background</figcaption></figure><p>Okay then! Now my first step is done. Now I should try to extract large vessels or small vessels. Then I could subtract it from all vessels and get the other vessel type. But since the small blood vessels are thin, I think it will be easy to remove small blood vessels other than large ones.</p><p>My initial idea was to extract small vessels by applying blurring and smoothing. But it didn’t work as I thought. So I have to move on to another method. Then I tried morphological transformation using structuring element of shape ellipse and I was able to remove small vessels. Then I found contours and remove the small components while drawing other contours. Following function will extract large vessels for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dff7a3462048355e440d32314745dc78/href">https://medium.com/media/dff7a3462048355e440d32314745dc78/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*3RSDyBofnLCn7hQrkDLaKA.jpeg" /><figcaption>after applying morphological transformation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*gmMhHmekSNQG9Xe4v5TpDQ.jpeg" /><figcaption>large blood vessels</figcaption></figure><p>Then by subtracting you can extract small vessels using following function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31c47e7ed19278e2250bcf8eb8e9086f/href">https://medium.com/media/31c47e7ed19278e2250bcf8eb8e9086f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*U7vYJTtIorZ7jtgFm4I1Sg.jpeg" /><figcaption>small blood vessels</figcaption></figure><p>As I already said this is my first try and it may not be the best pipeline for this. If you have something better than this leave a comment and let me know. At the end what I learnt was there is no theory or rules to find out the best pipeline. It is something you have to earn with experience and through researches.</p><p>Hope this <a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.html">tutorial about image processing operations</a> will help someone new to image processing with OpenCV.</p><p>Please visit my <a href="https://github.com/jpbinith/Extract_Blood_Vessels_from_Fundus_Image.git">Github repository</a> for the full code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44433d1fddb7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/what-i-learned-from-my-first-image-processing-project-44433d1fddb7">What I learned from my first Image Processing Project</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connect Express REST API with MongoDB and CRUD operations using TypeScript]]></title>
            <link>https://levelup.gitconnected.com/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/58d9afcc06d</guid>
            <category><![CDATA[express]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 18:44:43 GMT</pubDate>
            <atom:updated>2020-07-29T19:57:44.230Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*p-zoott5uZ8YR3ntSfz5vA.png" /></figure><p>Part 1: <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></p><p>Part 2: <a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></p><p>Part 3: <a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></p><p>A REST API needs a database to save our data, just like any application. A database can be a text file, SQL database (like MySQL or Post SQL) or NoSQL database (like MongoDB or Redis). In this article, you will learn how to connect your REST API with MongoDB. Please refer to my <a href="https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-4-954c8c059cd4">previous article</a> to install MongoDB to your machine. You can find a complete guide about how to build your REST API up to here in the following articles.</p><ol><li><a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API with Node.js Express</a></li><li><a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and building routes of REST API with Node.js Express</a></li></ol><p>The final aim of building an API is to host on a server and use it for mobile or web applications. So we build it in one environment and host in another environment. So it is a good practice if you manage those environments in a separate file. Then you can manage different port numbers, database names for each environment easily. Inside the <strong>lib</strong> folder create a file named <strong>environment.ts</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a02cb3f2b0b776950e6d5249db63eb6d/href">https://medium.com/media/a02cb3f2b0b776950e6d5249db63eb6d/href</a></iframe><p>In a <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">previous article</a>, we initialized a PORT number at the <strong>server.ts</strong> file. Now we can replace it with the getPort() method in environment class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88bf95ddc0fd9584e483ab3dd619075d/href">https://medium.com/media/88bf95ddc0fd9584e483ab3dd619075d/href</a></iframe><p>Okay then! Let’s start to work with <strong>Mongoose</strong>. Hope you have already installed the MongoDB to your machine. If not, follow <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">this article</a> to continue with us. If yes, now you are ready to connect the REST API to your local MongoDB. But if you want, you can connect any other database services as well.</p><p>First you need to import <strong>mongoose package</strong> and declare <strong>mongoUrl </strong>for your MongoDB in the <strong>app.ts</strong> file. Don’t forget to import environment.ts file and get the DB name through it. Now you can create a function named <strong>mongoSetup()</strong> and call it in the constructor. Therefore it calls through constructor every time when the server restarts and it will invoke <strong>mongo.connect()</strong> command to connects to a MongoDB instance and to a specified database on that instance. And now the <strong>app.ts</strong> file will be like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e51814da3161f141b518532715821fa/href">https://medium.com/media/0e51814da3161f141b518532715821fa/href</a></iframe><p>After this, your app is ready to do MongoDB CRUD (create. read, update, delete) operations. Let’s see what is the role of controllers and models in this process by creating a collection named ‘users’.</p><p>Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection(table) and defines the shape of the documents within that collection. So in order to access each collection(table) in the database we have to create a schema.</p><ul><li>Schema is a JSON object that allows you to define the shape and content of documents and embedded documents in a collection</li><li>service file have all database operation services</li><li>model file has the interfaces of users (Interfaces are capable of describing the wide range of shapes that JavaScript objects can take)</li></ul><p>First create a folder named <strong>modules</strong> inside the <strong>lib</strong> folder. There may some common interfaces or services which we need all over the program. we can make a folder named <strong>common</strong> and create files for commonly used interfaces and services. Inside it create two files named <strong>model.ts</strong> and <strong>service.ts</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53ac4e6d1617ab6d8a7636fb517bc88a/href">https://medium.com/media/53ac4e6d1617ab6d8a7636fb517bc88a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e3172943f67528147ad1384d3fa828a/href">https://medium.com/media/4e3172943f67528147ad1384d3fa828a/href</a></iframe><p>Then inside that folder create another folder named <strong>users. </strong>Always create this folder in the name of your collection. Inside that we can create schema, services and models of that entity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7c7d682d86fd66bb7ab63f2e166f23f/href">https://medium.com/media/f7c7d682d86fd66bb7ab63f2e166f23f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cf1bf9c6649615c9915be9473473305/href">https://medium.com/media/4cf1bf9c6649615c9915be9473473305/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/039856088749b2edf9f02dc356037525/href">https://medium.com/media/039856088749b2edf9f02dc356037525/href</a></iframe><p>The question mark (‘?’) used in the interface(<strong>IUser</strong>) members (<strong>_id</strong>, <strong>is_deleted</strong>) means that when a JSON object is created on the given interface type, that member is not necessary to be initialized.</p><p>Now we have finished the implementation of modules and now we can move on to the controllers.</p><blockquote>Why we need Controllers?</blockquote><p>Controllers handle the logic part of the system. Let&#39;s see how to write controller functions to create, read, update, and delete.</p><p>Now create a folder named <strong>controllers</strong> inside the <strong>lib</strong> folder. Inside that create a file named <strong>userController.ts</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f84d25588da8247205bef3a468174545/href">https://medium.com/media/f84d25588da8247205bef3a468174545/href</a></iframe><p>Now, create a folder named <strong>routes</strong> inside the <strong>lib</strong> folder. Inside that, create a file named <strong>user_routes.ts</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8d813385bb0357fe00b583dd46da97d/href">https://medium.com/media/b8d813385bb0357fe00b583dd46da97d/href</a></iframe><p>Now you can run the API and check the results.</p><h3>Create User</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iSjPLcZ3B-yGrlV56npaQQ.png" /><figcaption>Request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hUAnxOVlRbvwd3QV5fzdXA.png" /><figcaption>Response</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e2AzF8BUbl1Q-kjCG8c8Fg.png" /><figcaption>mongoDB</figcaption></figure><h3>Get User</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5mrihnapcNzhY6AXjVegaA.png" /></figure><h3>Update User</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WsKUZIbVsT5w4Na3YvoNoA.png" /></figure><p>Here we can update any field, and I hope you can understand the logic by looking at the code.</p><h3>Delete User</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Q0hx_SU5FW6jju95Qox8Q.png" /></figure><p>When deleting data, normally we do not remove the data completely from the database. We update a flag indicating that the particular data is deleted and keep it away when retrieving the data from the database. But in this tutorial, I deleted data completely because I wanted to show you how to do it. There are many more mongoDB queries and you can find the documentary <a href="https://docs.mongodb.com/manual/tutorial/query-documents/">here</a>.</p><p>This is the end of this article and I hope you understood everything well. In the next article, I’ll show you how to create a Login with user verification and managing user roles.</p><p>Please visit my <a href="https://github.com/jpbinith/test-project">Github repository</a> for the full code. If you are interested you can learn more about Node Express REST APIs from the following articles.</p><ol><li><a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></li><li><a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></li><li><a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58d9afcc06d" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect Express REST API with MongoDB and CRUD operations using TypeScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Project structure and Building routes of RESTFUL API with Node.js,]]></title>
            <link>https://levelup.gitconnected.com/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3a8b53d94e7</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[node]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Sun, 12 Apr 2020 19:51:58 GMT</pubDate>
            <atom:updated>2020-07-29T19:57:15.151Z</atom:updated>
            <content:encoded><![CDATA[<h3>Project structure and Building routes of RESTFUL API with Node.js, Express, MongoDB using TypeScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*p-zoott5uZ8YR3ntSfz5vA.png" /></figure><p>Part 1: <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></p><p>Part 2: <a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></p><p>Part 3: <a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></p><p>In this article, I will discuss how to maintain the project structure for best practice and how to implement the routes for your first API. Hope you’ll enjoy it. 😃</p><p>Before following this article make sure to follow my article about <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">how to setup your first REST API</a>.</p><p>Express is a great framework for making a Node.js REST APIs however it doesn’t give you any clue on how to organize your project.</p><p>You are free to use any structure you want. But when your codebase grows you end up having long handlers. This makes your code hard to understand and it contains potential bugs.</p><p>The correct organization of your project structure will improve stability and efficiency, avoid duplication of code, and help you scale your services if it is done correctly.</p><p>Here is the project structure that I’m talking about. I use this in every REST API service that I build, let’s see in details what every component do.</p><pre>backend<br>│<br>└─── dist                   #all the javascript files are here<br>└─── lib                    #all the typescript files are here<br>      └─── config           #app configurations files<br>            └─── app.ts     #app starting point            <br>      └─── controllers      #request managers<br>      └─── modules          #schemas, interfaces, services<br>      └─── routes           #define the endpoints<br>      └─── environment.ts   #store all environment variables<br>      └─── server.js        #HTTP server that listens to server port<br>└─── .gitignore             #git ignore file<br>└─── package-lock.json      #npm automatically generated document<br>└─── package.json           #holds metadata and npm packagage list<br>└─── tsconfig.json          #specify the root level files and the                                              .                            compiler options</pre><p>Here I preform MongoDB configurations inside the app.ts file. If you want you can do it in a separate file also. It would be better if you find the most suitable project structure for you and maintain it in your every project for the best practices.</p><p>Let&#39;s move on to the next part, building routes for your first RESTFUL API.</p><p>As I told in the <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">previous article</a> since we are developing with typescript we do all the implements inside the ‘<strong>lib</strong>’ folder. So first you have to create a folder named ‘<strong>routes</strong>’ inside the ‘<strong>lib</strong>’ folder. Then create a file inside ‘<strong>routes</strong>’ folder and name it as ‘<strong>test_routes.ts</strong>’. Inside that file copy and paste the following.</p><pre>//lib/routes/test_routes.ts<br>import {Application, Request, Response } from &#39;express&#39;;</pre><pre>export class TestRoutes {</pre><pre>   public route(app: Application) {</pre><pre>      app.get(&#39;/api/test&#39;, (req: Request, res: Response) =&gt; {<br>         res.status(200).json({message: &quot;Get request successfull&quot;});<br>      });</pre><pre>      app.post(&#39;/api/test&#39;, (req: Request, res: Response) =&gt; {<br>         res.status(200).json({message:&quot;Post request successfull&quot;});<br>      });</pre><pre>   }</pre><pre>}</pre><p>So now we have finished building our first route with two endpoints. But, still it wouldn’t be working properly. Why? Just because we haven’t connected it to the app module. Still this is a external module and the app doesn’t know the existence of this module. So we have to tell the app to include this module. let’s see how to do it. After we finish the integration and call the relevant endpoints, it will respond with a status code 200.</p><p>Let&#39;s move on to the ‘<strong>app.ts</strong>’ file in ‘<strong>config</strong>’ folder. Then include the following highlighted lines.</p><pre>//lib/config/app.ts<br>import * as express from &quot;express&quot;;<br>import * as bodyParser from &quot;body-parser&quot;;<br><strong>import { TestRoutes } from &quot;../routes/test_routes&quot;;</strong></pre><pre>class App {</pre><pre>   public app: express.Application;<br>   <strong>private test_routes: TestRoutes = new TestRoutes();</strong></pre><pre>   constructor() {<br>      this.app = express();<br>      this.config();<br>      <strong>this.test_routes.route(this.app);</strong><br>   }</pre><pre>   private config(): void {<br>      // support application/json type post data<br>      this.app.use(bodyParser.json());<br>      //support application/x-www-form-urlencoded post data<br>      this.app.use(bodyParser.urlencoded({ extended: false }));<br>   }<br>}<br>export default new App().app;</pre><p>Here we create an instance of ‘<strong>TestRoutes</strong>’ and import it to the constructor of the app module. Now when the app runs it knows the existence of the route. Let’s start our API by running ‘<strong>npm run dev</strong>’ on the terminal. If you already started the API using ‘<strong>npm run prod</strong>’ you can restart it by running ‘<strong>tsc</strong>’ on another terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CMGLK79-z05AxViU7HicUQ.png" /></figure><p>Let’s send a GET and POST request to the API using <a href="https://www.getpostman.com/apps">Postman</a> and see the results. (http://localhost:3000/api/test)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rANHj-_aKB66k5HNrvKvoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rANHj-_aKB66k5HNrvKvoQ.png" /></figure><p>So we can see our end points (http://localhost:3000/api/test) are working perfectly. Let’s see what happen if we send request to anything other than this endpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pMuCbCzZR2NMNVdUXKkiSQ.png" /></figure><p>So it will return a error message with a status 404. Let’s see how to handle it.</p><p>Inside the ‘<strong>routes</strong>’ folder create another file named ‘<strong>common_routes.ts</strong>’ and copy and paste following.</p><pre>//lib/routes/common_routes.ts<br>import { Application, Request, Response } from &#39;express&#39;;</pre><pre>export class CommonRoutes {</pre><pre>   public route(app: Application) {</pre><pre>      // Mismatch URL<br>      app.all(&#39;*&#39;, function (req: Request, res: Response) {<br>         res.status(404).send({ error: true, message: &#39;Check your URL please&#39; });</pre><pre>      });<br>   }<br>}</pre><p>Now add it to the ‘<strong>app.ts</strong>’ file as above. But make sure to keep it always as the last route inside the constructor. Now run it and see the results.</p><blockquote><strong>Use of CRUD?</strong></blockquote><p>CRUD stands for Create, Read, Update, and Delete. with regards to its use in REST API, CRUD is the standardized use of HTTP request.</p><ul><li>GET: for retrieving data</li><li>POST: for creating new data</li><li>PUT / PATCH: for updating data</li><li>DELETE: for deleting data</li></ul><blockquote><strong>Use of Status Codes?</strong></blockquote><p>When monitoring,it is easier to know the health of an application by its status code. However, for some REST services the HTTP status code is not well defined.</p><p>The most common codes to use for any HTTP request are:<br>200 — OK<br>404 — Not found<br>500 — Internal Server Error</p><p>One can then expand to a more detailed set of statuses if required, such as:<br>201 — Created<br>204 — No Content<br>304 — Modified<br>400 — Bad Request<br>401 — Unauthorized<br>403 — Forbidden<br>501 — Not Implemented</p><p>Even you can use any code for any status. But it’s better if you follow the standards for the best practice.</p><p>This is the end of the this article hope you understood everything well. See you with the next article about implementing MongoDB and also going to talk about using the controllers too.</p><p>Please visit my <a href="https://github.com/jpbinith/test-project">Github repository</a> for the full code. If you are interested you can learn move about Node Express REST APIs from following articles.</p><ol><li><a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></li><li><a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></li><li><a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3a8b53d94e7" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building routes of RESTFUL API with Node.js,</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup RESTful API with Node.js, Express, MongoDB using TypeScript]]></title>
            <link>https://levelup.gitconnected.com/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998?source=rss-fdc0ed0d3401------2</link>
            <guid isPermaLink="false">https://medium.com/p/261959ef0998</guid>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[expressjs]]></category>
            <dc:creator><![CDATA[Binith Jayasinghe]]></dc:creator>
            <pubDate>Sat, 04 Apr 2020 12:47:17 GMT</pubDate>
            <atom:updated>2020-07-29T19:56:19.880Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*p-zoott5uZ8YR3ntSfz5vA.png" /></figure><p>Part 1: <a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></p><p>Part 2: <a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></p><p>Part 3: <a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></p><p>In this article I will help you to set up your first REST API using the above technologies and run it on your machine. Hope you’ll enjoy it. 😃</p><p>Before we start, let&#39;s have some idea about the technologies and tools that we are going to use.</p><h4>What is API and how it works?</h4><p>An API (Application Programming Interface) is something like a messenger. It acts as an intermediate between two or more apps that need to talk to each other. In other words, it delivers your requests to the provider ( what you are requesting from them) and there Response (the things you request) to you. So it is a mechanism that allows the interaction between two applications using a<strong> set of rules.</strong></p><p>Imagine you are in a restaurant to enjoy some meals. You have a menu on your table with a lot of choices to order and in the kitchen, there is a chef who is ready to prepare your meal. But the chef can’t come and take your order because he/she is busy with cooking in the kitchen. So the waiter will take order from you and deliver it to the kitchen, telling the chef what you order (request). And when the food (response) is ready it is delivered to you by the waiter. In here, the waiter act as an API. This example will give you a basic idea of what is an API and how it will work.</p><h4><strong>Types of APIs?</strong></h4><ul><li>SOAP</li><li>XML-RPC</li><li>JSON-RPC</li><li>REST</li></ul><h4>What is Rest API and why we use it?</h4><p>A REST (<strong>RE</strong>presentational <strong>S</strong>tate <strong>T</strong>ransfer) API is an application program interface that uses HTTP requests to GET, PUT, POST and DELETE data. It is not a protocol like the other web services, instead, it is a set of architectural principles. REST suggests to create an object of the data requested by the client and send the state of the object in response to the user. REST APIs are speed and agile than other types and developers like REST because the learning curve is less steep.</p><h4>Node.js</h4><p>Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.</p><h4>Express</h4><p>Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.</p><h4>MongoDB</h4><p>MongoDB is a document-oriented NoSQL database used for high volume data storage. MongoDB uses JSON-like documents with a schema.</p><h4>TypeScript</h4><p>TypeScript is an open-source programming language developed and maintained by Microsoft. It is a superset of the JavaScript language, and adds optional static typing to the language. The goal of TypeScript is to help catch mistakes early through a type system and to make JavaScript development more efficient.</p><h4>MEAN?</h4><p>In MEAN stack, the M stands for MongoDB, E for Express, A for AngularJS, and ultimately N stands for Node.js. Angular is a front-end development framework and since we are focusing on back-end I’m not going to talk about it here. MEAN stack is famous than other competitors because it covers the full web development cycle from front-end to back-end development using javascript, helps to keep the web application development much organized, resist unnecessary grunt work and have good community supports because they are open source.</p><p>Before we get started, Make sure that you have <a href="https://nodejs.org/en/">NodeJS,</a> <a href="https://docs.mongodb.com/manual/administration/install-community/">MongoDB</a><a href="https://nodejs.org/en/"> </a>installed on your machine. If you installed MongoDB locally, you should install either <a href="https://docs.mongodb.com/compass/master/install/">Mongo Compass</a> or <a href="https://robomongo.org/">Robo 3T</a> for GUI interface. And I suggest you to use <a href="https://code.visualstudio.com/">VS CODE</a> for the developments.</p><p>It’s better if you install TypeScript globally before start our project. Then type the following line in your terminal and press Enter.</p><pre>npm install -g typescript ts-node</pre><p>In order to test HTTP requests, we can use <a href="https://www.getpostman.com/apps">Postman </a>to send sample requests and see the response.</p><p>Now we can sink in to the coding part 💪 Lets setup our project first</p><blockquote><strong>Step 1: Initiate a Node project</strong></blockquote><p>Open the folder where you want to create your project. Then click on the address bar of your file explore type cmd and enter. Then on your terminal type ‘code .’ and it will direct you to vs code. In vs code you can get terminal open by click on terminal tab and select new terminal or by pressing ‘ctrl + shift +` ‘. You can answer the questions now or can edit it any time after that. After you finish initiate you can see ‘<strong>package.json</strong>’ has been created.</p><pre>mkdir my-first-project<br>cd .\my-first-project\<br>npm init</pre><blockquote><strong>Step 2: Install all the dependencies</strong></blockquote><p>After finish this you can see ‘<strong>package-lock.json</strong>’ file created and ‘<strong>node_modules</strong>’ has been imported.</p><pre>npm install --save @types/express express body-parser mongoose   nodemon</pre><blockquote><strong>Step 3: Configure the TypeScript configuration file</strong></blockquote><p>We do development using typescript but since we run it on Node.js we need to convert it to javascript. So here we put our all typescript files inside a folder name <strong><em>lib </em></strong>and java script files inside a folder name <strong><em>dist</em></strong>. Lets talk about this file format in the next article. Now create a file inside your project and name it as ‘<strong>tsconfig.json</strong>’ and copy and paste following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d0f600145fcec91648eacbd236d029f/href">https://medium.com/media/5d0f600145fcec91648eacbd236d029f/href</a></iframe><p>Whenever we run typescript compiler ‘<strong>tsc</strong>’ all the typescript files inside ‘<strong>lib</strong>’ folder will convert into javascript and save inside ‘<strong>dist</strong>’ folder</p><pre>tsc</pre><blockquote><strong>Step 4: Set the scripts to run API</strong></blockquote><p>Inside ‘<strong>package.json</strong>’ file update main file as follow.</p><pre>&quot;main&quot;: &quot;./dist/server.js&quot;</pre><p>Inside ‘<strong>package.json</strong>’ file under the scripts add the following scripts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc71505948a36498eb6811375a7ce130/href">https://medium.com/media/bc71505948a36498eb6811375a7ce130/href</a></iframe><p>So, for the development, we can run a test server by running following on your terminal</p><pre>npm run test / npm run dev</pre><p>For the production</p><pre>npm run prod</pre><p>Here we compile the typescript to javascript using typescript compiler (tsc) and go to the direction where server.ts file locate and run it using nodemon.</p><p>To terminate the process, press</p><pre>ctrl + c</pre><blockquote><strong>Step 5: Configure base configurations on app.ts</strong></blockquote><p>Just for now lets create another folder named ‘<strong>config</strong>’ inside the ‘<strong>lib</strong>’ folder. In the next article we’ll discus more about the file format. And inside this ‘<strong>config</strong>’ folder create a file named ‘<strong>app.ts</strong>’ and copy and paste following in it. Parse incoming request bodies in a middleware before your handlers, available under the req.body property. For now we are not going to integrate mongo on this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/320f5e7f4245b7145c739f7b3111ce0d/href">https://medium.com/media/320f5e7f4245b7145c739f7b3111ce0d/href</a></iframe><p>Then create server.ts file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8db1d49186af780e54e3e7a0b038237b/href">https://medium.com/media/8db1d49186af780e54e3e7a0b038237b/href</a></iframe><p>From now you can test the project by running ‘<strong>npm run dev</strong>’ and following will print in your terminal.</p><blockquote>Express server listening on port 3000</blockquote><p>Please visit my <a href="https://github.com/jpbinith/test-project">Github repository</a> for the full code. If you are interested you can learn move about Node Express REST APIs from following articles.</p><ol><li><a href="https://medium.com/@jpbinith/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup REST API</a></li><li><a href="https://medium.com/@jpbinith/project-structure-and-building-routes-of-restful-api-with-node-js-f3a8b53d94e7">Project structure and Building Routes in REST API</a></li><li><a href="https://medium.com/@jpbinith/connect-express-rest-api-with-mongodb-and-crud-operations-using-typescript-58d9afcc06d">Connect to MongoDB and CRUD operations</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=261959ef0998" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/setup-restful-api-with-node-js-express-mongodb-using-typescript-261959ef0998">Setup RESTful API with Node.js, Express, MongoDB using TypeScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>