<?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: Diego Guevara</title>
    <description>The latest articles on DEV Community by Diego Guevara (@diegoguevara).</description>
    <link>https://dev.to/diegoguevara</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%2F491182%2F07bba7c0-682b-4364-a0c6-f8144acea70c.png</url>
      <title>DEV Community: Diego Guevara</title>
      <link>https://dev.to/diegoguevara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/diegoguevara"/>
    <language>en</language>
    <item>
      <title>Serverless functions with Vercel</title>
      <dc:creator>Diego Guevara</dc:creator>
      <pubDate>Thu, 11 Mar 2021 20:26:54 +0000</pubDate>
      <link>https://dev.to/diegoguevara/serverless-functions-with-vercel-287n</link>
      <guid>https://dev.to/diegoguevara/serverless-functions-with-vercel-287n</guid>
      <description>&lt;p&gt;With Vercel, you can deploy Serverless Functions, which are pieces of code written with backend languages like NodeJS, that take an HTTP request and provide a response.&lt;/p&gt;

&lt;p&gt;You can use Serverless Functions to handle user authentication, form submission, database queries, custom slack commands, and more.&lt;/p&gt;

&lt;p&gt;In this article, we'll create a simple Serverless function with NodeJS, and then deploy it in Vercel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create project with an API endpoint
&lt;/h2&gt;

&lt;p&gt;Initialize &lt;code&gt;npm&lt;/code&gt; project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to create a folder called &lt;code&gt;/api&lt;/code&gt; where our API endpoint files will be.&lt;/p&gt;

&lt;p&gt;In this example, we are going to create a file called &lt;code&gt;hello.js&lt;/code&gt;, with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = (req, res) =&amp;gt; {
    res.json({
        hola: 'mundo'    
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your project now looks like this&lt;/p&gt;

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

&lt;p&gt;In this example our endpoint service will respond with a JSON with the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    hola: 'mundo'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploy to Vercel
&lt;/h2&gt;

&lt;p&gt;Previously you need to install and configure &lt;a href="https://vercel.com/download" rel="noopener noreferrer"&gt;Vercel CLI&lt;/a&gt;.&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 -g vercel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In terminal, at the root of the project write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ vercel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Now in the Vercel web dashboard you'll see your project and the project URL&lt;/p&gt;

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

&lt;p&gt;Now, let's test our service in browser, go to the project URL, and remember to add the API Path, in this case is &lt;code&gt;/api/hello&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;That's all... Now it's your turn, create all the endpoints you need in your API, just remember that each endpoint is a file.&lt;/p&gt;

&lt;p&gt;thanks for reading me... and Happy coding ...&lt;/p&gt;

</description>
      <category>node</category>
      <category>vercel</category>
      <category>serverless</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
