<?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: Devansh jain</title>
    <description>The latest articles on DEV Community by Devansh jain (@devanshjaincampaigntech).</description>
    <link>https://dev.to/devanshjaincampaigntech</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%2F3981233%2F712903f8-14ab-4b94-9a2e-de901bde571a.jpeg</url>
      <title>DEV Community: Devansh jain</title>
      <link>https://dev.to/devanshjaincampaigntech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devanshjaincampaigntech"/>
    <language>en</language>
    <item>
      <title>How I Built and Deployed a URL Shortener API as a 2nd Year CS Student</title>
      <dc:creator>Devansh jain</dc:creator>
      <pubDate>Fri, 12 Jun 2026 12:31:03 +0000</pubDate>
      <link>https://dev.to/devanshjaincampaigntech/how-i-built-and-deployed-a-url-shortener-api-as-a-2nd-year-cs-student-2i1o</link>
      <guid>https://dev.to/devanshjaincampaigntech/how-i-built-and-deployed-a-url-shortener-api-as-a-2nd-year-cs-student-2i1o</guid>
      <description>&lt;p&gt;I am a second year B.Tech Computer Science student at NIT Patna. &lt;br&gt;
Two weeks ago I had never built or deployed a real backend project. &lt;br&gt;
Today I have a live API running on the internet that anyone can use.&lt;/p&gt;

&lt;p&gt;This is the story of how I built it, what I learned, and the specific &lt;br&gt;
mistakes I made along the way.&lt;/p&gt;


&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;A URL Shortener API. You give it a long URL. It gives you a short code. &lt;br&gt;
When someone visits the short link, they get redirected to the original URL.&lt;/p&gt;

&lt;p&gt;Sounds simple. Building it properly was not.&lt;/p&gt;

&lt;p&gt;Live URL: &lt;a href="https://urlshortener-api-production.up.railway.app/health" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app/health&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub: &lt;a href="https://urlshortener-api-production.up.railway.app" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  The Tech Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node.js and Express for the server&lt;/li&gt;
&lt;li&gt;PostgreSQL for the database&lt;/li&gt;
&lt;li&gt;JWT for authentication&lt;/li&gt;
&lt;li&gt;bcrypt for password hashing&lt;/li&gt;
&lt;li&gt;nanoid for generating short codes&lt;/li&gt;
&lt;li&gt;Docker for containerization&lt;/li&gt;
&lt;li&gt;Railway for deployment&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  The Architecture — Why MVC Matters
&lt;/h2&gt;

&lt;p&gt;The first version of my project had everything in one file. Routes, &lt;br&gt;
database queries, validation, business logic — all 200 lines in app.js.&lt;/p&gt;

&lt;p&gt;It worked. But it was unreadable.&lt;/p&gt;

&lt;p&gt;I refactored into MVC architecture. Model View Controller. &lt;br&gt;
Here is what that actually means in practice:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Models&lt;/strong&gt; handle all database queries. Nothing else. &lt;br&gt;
They know nothing about HTTP requests or responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Controllers&lt;/strong&gt; handle the request, call models for data, &lt;br&gt;
and send the response. They never write SQL directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routes&lt;/strong&gt; just map URLs to controller functions. &lt;br&gt;
A route file should be readable in 30 seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Middleware&lt;/strong&gt; handles cross-cutting concerns. &lt;br&gt;
JWT verification, input validation, rate limiting.&lt;/p&gt;

&lt;p&gt;My folder structure ended up looking like this:&lt;/p&gt;

&lt;p&gt;src/&lt;/p&gt;

&lt;p&gt;├── config/&lt;/p&gt;

&lt;p&gt;│   └── db.js&lt;/p&gt;

&lt;p&gt;├── models/&lt;/p&gt;

&lt;p&gt;│   ├── userModel.js&lt;/p&gt;

&lt;p&gt;│   └── urlModel.js&lt;/p&gt;

&lt;p&gt;├── controllers/&lt;/p&gt;

&lt;p&gt;│   ├── authController.js&lt;/p&gt;

&lt;p&gt;│   └── urlController.js&lt;/p&gt;

&lt;p&gt;├── routes/&lt;/p&gt;

&lt;p&gt;│   ├── authRoutes.js&lt;/p&gt;

&lt;p&gt;│   └── urlRoutes.js&lt;/p&gt;

&lt;p&gt;├── middleware/&lt;/p&gt;

&lt;p&gt;│   ├── auth.js&lt;/p&gt;

&lt;p&gt;│   ├── validate.js&lt;/p&gt;

&lt;p&gt;│   └── rateLimiter.js&lt;/p&gt;

&lt;p&gt;└── utils/&lt;/p&gt;

&lt;p&gt;└── generateCode.js&lt;/p&gt;

&lt;p&gt;When something breaks now, I know exactly which file to open. &lt;br&gt;
That is what clean architecture gives you.&lt;/p&gt;


&lt;h2&gt;
  
  
  How JWT Authentication Works in This Project
&lt;/h2&gt;

&lt;p&gt;Most tutorials show you how to use JWT. Very few explain why &lt;br&gt;
each decision was made.&lt;/p&gt;

&lt;p&gt;Here is my implementation and the reasoning behind it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why two tokens instead of one?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Access tokens expire in 15 minutes. Refresh tokens expire in 7 days.&lt;/p&gt;

&lt;p&gt;If an access token is stolen, the attacker only has 15 minutes. &lt;br&gt;
Short enough to limit damage. Long enough that users are not &lt;br&gt;
logging in every 15 minutes.&lt;/p&gt;

&lt;p&gt;Refresh tokens are stored in the database. When a user logs out, &lt;br&gt;
the refresh token is deleted. The next time someone tries to use &lt;br&gt;
that refresh token, it fails. That is real logout functionality.&lt;/p&gt;

&lt;p&gt;With a single long-lived token, you cannot log someone out. &lt;br&gt;
The token works until it expires regardless.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why bcrypt for passwords?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;bcrypt is intentionally slow. It takes about 300 milliseconds &lt;br&gt;
to hash one password.&lt;/p&gt;

&lt;p&gt;That sounds like a problem. It is actually the feature.&lt;/p&gt;

&lt;p&gt;SHA-256 computes billions of hashes per second. An attacker &lt;br&gt;
with a stolen database can try billions of password guesses per second.&lt;/p&gt;

&lt;p&gt;bcrypt limits them to thousands per second. The math makes &lt;br&gt;
brute force attacks impractical.&lt;/p&gt;

&lt;p&gt;I used a cost factor of 12. That means 4096 iterations per hash.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Bug That Took the Longest to Fix
&lt;/h2&gt;

&lt;p&gt;I installed Express version 5 without realizing it.&lt;/p&gt;

&lt;p&gt;In Express 4, you could write routes with inline regex like this:&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;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;/:code([a-zA-Z0-9]{6,10})&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Express 5 removed this syntax completely. My server crashed &lt;br&gt;
immediately on startup with a PathError that looked like this:&lt;/p&gt;

&lt;p&gt;PathError [TypeError]: Unexpected ( at index 6: /:code([a-zA-Z0-9]{6,10})&lt;/p&gt;

&lt;p&gt;I spent 30 minutes reading the error message before I realized &lt;br&gt;
the problem was the Express version, not my regex.&lt;/p&gt;

&lt;p&gt;The fix was downgrading to Express 4 and moving the validation &lt;br&gt;
into the controller function instead:&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;// Now in the controller&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;validCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-zA-Z0-9&lt;/span&gt;&lt;span class="se"&gt;]{4,20}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;code&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;validCode&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;res&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="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid short code&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;One line of code. 30 minutes to find where to put it.&lt;/p&gt;

&lt;p&gt;That experience taught me something important. When you see &lt;br&gt;
a confusing error, check your dependency versions before &lt;br&gt;
assuming the bug is in your logic.&lt;/p&gt;


&lt;h2&gt;
  
  
  The nanoid Problem
&lt;/h2&gt;

&lt;p&gt;I installed nanoid version 5. My project uses CommonJS with require.&lt;/p&gt;

&lt;p&gt;nanoid version 4 and above only supports ES Modules. &lt;br&gt;
They removed CommonJS support.&lt;/p&gt;

&lt;p&gt;When I tried to use it I got this error:Error [ERR_REQUIRE_ESM]: require() of ES Module not supported&lt;/p&gt;

&lt;p&gt;The fix was downgrading to nanoid version 3, &lt;br&gt;
the last version with CommonJS support:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;nanoid@3.3.6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the kind of thing no tutorial warns you about. &lt;br&gt;
You only learn it by hitting the wall yourself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deploying to Railway
&lt;/h2&gt;

&lt;p&gt;Deployment was the part I was most nervous about.&lt;/p&gt;

&lt;p&gt;It turned out to be the easiest part.&lt;/p&gt;

&lt;p&gt;Steps I took:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pushed code to GitHub&lt;/li&gt;
&lt;li&gt;Connected Railway to my GitHub repository&lt;/li&gt;
&lt;li&gt;Added a PostgreSQL database on Railway&lt;/li&gt;
&lt;li&gt;Copied database credentials into Railway environment variables&lt;/li&gt;
&lt;li&gt;Created the tables using Railway's built-in console&lt;/li&gt;
&lt;li&gt;Generated a domain&lt;/li&gt;
&lt;li&gt;Tested the live URL&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The whole process took about 45 minutes.&lt;/p&gt;

&lt;p&gt;The most important thing I learned about deployment: &lt;br&gt;
your .env file never goes to GitHub. Ever. &lt;br&gt;
Environment variables are configured separately on the server.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the Live API Can Do
&lt;/h2&gt;

&lt;p&gt;You can test it right now.&lt;/p&gt;

&lt;p&gt;Health check:GET &lt;a href="https://urlshortener-api-production.up.railway.app/health" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app/health&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Register an account:POST &lt;a href="https://urlshortener-api-production.up.railway.app/api/auth/register" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app/api/auth/register&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Body: { "name": "Your Name", "email": "&lt;a href="mailto:you@example.com"&gt;you@example.com&lt;/a&gt;", "password": "password123" }&lt;/p&gt;

&lt;p&gt;Shorten a URL after logging in:&lt;/p&gt;

&lt;p&gt;POST &lt;a href="https://urlshortener-api-production.up.railway.app/api/urls/shorten" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app/api/urls/shorten&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Authorization: Bearer your_token_here&lt;/p&gt;

&lt;p&gt;Body: { "url": "&lt;a href="https://www.google.com" rel="noopener noreferrer"&gt;https://www.google.com&lt;/a&gt;" }&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Backend is not about knowing frameworks. It is about understanding problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before this project I thought Express was magic. Now I know &lt;br&gt;
it is just a wrapper around Node's http module that makes &lt;br&gt;
routing and middleware easier.&lt;/p&gt;

&lt;p&gt;Before this project I thought JWT was complicated. Now I can &lt;br&gt;
implement it from scratch without looking anything up.&lt;/p&gt;

&lt;p&gt;Before this project I thought deployment meant buying a server. &lt;br&gt;
Now I know free hosting exists and it takes less than an hour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The five most important things I actually learned:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;MVC architecture is not optional for real projects. &lt;br&gt;
It is the difference between code you can maintain &lt;br&gt;
and code you are afraid to touch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Never concatenate user input into SQL queries. &lt;br&gt;
Always use parameterized queries. &lt;br&gt;
SQL injection is real and trivially easy to exploit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Environment variables are not optional. &lt;br&gt;
Your secrets never go in your code. Never.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Read error messages carefully before assuming &lt;br&gt;
the bug is in your logic. Half the time &lt;br&gt;
it is a version conflict or a misconfiguration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploy early. A project that only runs on localhost &lt;br&gt;
does not exist from anyone else's perspective.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What is Next
&lt;/h2&gt;

&lt;p&gt;I am building a Task Manager API with teams, roles, &lt;br&gt;
and email notifications as my next project.&lt;/p&gt;

&lt;p&gt;New things I am learning: many-to-many database relationships, &lt;br&gt;
role-based access control, file uploads with Multer, &lt;br&gt;
and sending emails from a backend with Nodemailer.&lt;/p&gt;

&lt;p&gt;Follow me here on dev.to or on LinkedIn if you want to &lt;br&gt;
follow the journey.&lt;/p&gt;

&lt;p&gt;If you are a CS student who has been putting off building &lt;br&gt;
your first project — this post is me telling you to just start. &lt;br&gt;
The gap between knowing and building is smaller than you think.&lt;/p&gt;




&lt;p&gt;GitHub: &lt;a href="https://github.com/devanshjaincampaign-tech/url_shortener-api" rel="noopener noreferrer"&gt;https://github.com/devanshjaincampaign-tech/url_shortener-api&lt;/a&gt;&lt;br&gt;
Live API: &lt;a href="https://urlshortener-api-production.up.railway.app/health" rel="noopener noreferrer"&gt;https://urlshortener-api-production.up.railway.app/health&lt;/a&gt;&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/devansh-jain-314208375/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/devansh-jain-314208375/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>learning</category>
      <category>node</category>
    </item>
  </channel>
</rss>
