DEV Community

Cover image for Stop Copying ObjectIDs: Use Slugify in Your Node.js API (E-Commerce)
Dorcas Nyamekye Quaye
Dorcas Nyamekye Quaye

Posted on

Stop Copying ObjectIDs: Use Slugify in Your Node.js API (E-Commerce)

I used to spend extra time copying database ObjectIDs when testing my endpoints. I'd be creating categories, products, and all kinds of CRUD operations, only to dig through the database for IDs every time.

Then I discovered slugify, a small Node.js package that makes URLs readable, SEO-friendly, and much easier to test. Suddenly, instead of typing /categories/6489f3a2b6c1f7a12345, I could just type /categories/mens-clothing and keep moving.


What is Slugify?

Slugify is a simple Node.js package that converts strings into URL-friendly “slugs.” It’s user-friendly, lowercase, hyphenates spaces, and removes unsafe characters, making URLs clean and readable.

For example:

"Men's Clothing" → "mens-clothing"
"Café & Restaurant!" → "cafe-restaurant"
Enter fullscreen mode Exit fullscreen mode

This is especially useful for categories, products, or any resource you want to make readable in a URL.


Don’t Confuse Slugify with Basic String Methods

It’s important not to confuse slugify with just using basic string methods like trim() or toLowerCase().

  • trim() only removes spaces at the start or end of a string:
"  Men's Clothing  ".trim(); // "Men's Clothing"
Enter fullscreen mode Exit fullscreen mode
  • toLowerCase() only converts letters to lowercase, but leaves spaces and special characters untouched:
"Men's Clothing".toLowerCase(); // "men's clothing"
Enter fullscreen mode Exit fullscreen mode

If you combine them:

"  Men's Clothing  ".trim().toLowerCase(); // "men's clothing"
Enter fullscreen mode Exit fullscreen mode

You still end up with spaces and special characters, which isn’t URL-friendly.

Slugify, on the other hand, does all of this in one step:

  • Converts to lowercase
  • Replaces spaces with hyphens
  • Removes or replaces unsafe characters
import slugify from 'slugify';

slugify("  Men's Clothing  ", { lower: true }); 
// "mens-clothing"
Enter fullscreen mode Exit fullscreen mode

Now the string is clean, readable, and ready for a URL.


Why Use Slugify in E-commerce?

  1. SEO-Friendly URLs – Search engines prefer meaningful, readable URLs.
  2. Better UX – Users can read and remember URLs easily.
  3. Simpler Routing – Fetch resources by slug instead of ID.
  4. Easier Testing & Debugging – From a developer perspective, using slugs saves you from copying database IDs every time you test an endpoint. You can type a readable slug like mens-clothing and test your routes faster.

For me, this made testing category endpoints much simpler. Every category I create now has a slug, and I can remember or type it easily without digging into the database.


How I Implemented It in Node.js

First, install the package:

npm install slugify
Enter fullscreen mode Exit fullscreen mode

Then, use it when creating a new category:

import slugify from 'slugify';

const categoryName = "Men's Clothing";

//convert to slug
const categorySlug = slugify(categoryName, { lower: true });

//save in DB
const newCategory = await Category.create({ 
  name: categoryName, 
  slug: categorySlug 
});
Enter fullscreen mode Exit fullscreen mode

Now, instead of /categories/6489f3a2b6c1f7a12345, your URL looks like:

/categories/mens-clothing
Enter fullscreen mode Exit fullscreen mode

Some Use Cases of Slugify:


Takeaway

Slugify isn’t just about SEO or user-friendly URLs—it’s also a huge help for backend developers. It makes testing, debugging, and working with CRUD endpoints much simpler.

If you’re building categories, products, or other resources for a web app, I highly recommend giving it a try. Clean URLs, easier testing, happy developers, happy users!

Top comments (0)