Bird
Raised Fist0
Node.jsframework~10 mins

Resource naming conventions in Node.js - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Resource naming conventions
Start
↓
Identify resource type
↓
Choose naming style
↓
Apply naming rules
↓
Check consistency
↓
Use in code
↓
End
This flow shows how to pick and apply consistent resource names in Node.js projects.
Execution Sample
Node.js
const userController = require('./controllers/userController');
const productRouter = require('./routes/productRouter');

app.use('/users', userController);
app.use('/products', productRouter);
This code imports and uses resource files with consistent naming for controllers and routes.
Execution Table
StepActionResource NameNaming StyleResult
1Identify resource typeUser controllercamelCaseuserController
2Identify resource typeProduct routercamelCaseproductRouter
3Apply file namingUser controller filecamelCase + suffixuserController.js
4Apply file namingProduct router filecamelCase + suffixproductRouter.js
5Use in code importImport user controllervariable matches fileconst userController = require('./controllers/userController');
6Use in code importImport product routervariable matches fileconst productRouter = require('./routes/productRouter');
7Use in appMount user controllerURL path plural nounapp.use('/users', userController);
8Use in appMount product routerURL path plural nounapp.use('/products', productRouter);
9Check consistencyAll namesConsistent camelCase and plural URLsNaming conventions followed
10End--Resources named and used consistently
💡 All resource names follow camelCase for variables and plural nouns for URL paths, ensuring clarity and consistency.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5After Step 6Final
userControllerundefineduserControlleruserControlleruserControlleruserControlleruserController
productRouterundefinedundefinedproductRouterundefinedproductRouterproductRouter
Key Moments - 3 Insights
Why do we use camelCase for resource variable names instead of snake_case or PascalCase?
camelCase is the common JavaScript convention for variables and functions, making code easier to read and consistent with community standards, as shown in steps 1, 2, 5, and 6.
Why are URL paths plural nouns like '/users' and '/products'?
Plural nouns in URLs represent collections of resources, which is a RESTful convention that helps users and developers understand the API structure, as seen in steps 7 and 8.
What happens if resource names are inconsistent between files and variables?
Inconsistent names cause confusion and errors when importing or using resources, breaking the code. Steps 5 and 6 show matching names to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the variable name used for the user controller after step 1?
AuserController
BUserController
Cuser_controller
Dusercontroller
💡 Hint
Check the 'Resource Name' column at step 1 in the execution_table.
At which step does the code mount the product router to the URL path?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look for 'Mount product router' action in the execution_table.
If we changed the URL path from '/users' to '/user', how would the naming convention be affected?
AIt would still follow plural noun convention
BIt would break the plural noun convention for URLs
CIt would change variable names to singular
DIt would require camelCase changes
💡 Hint
Refer to the key moment about URL path plural nouns and steps 7 and 8.
Concept Snapshot
Resource Naming Conventions in Node.js:
- Use camelCase for variable and file names (e.g., userController.js)
- Use plural nouns for URL paths (e.g., '/users')
- Keep variable names matching file names for imports
- Consistency improves readability and avoids errors
- Follow RESTful conventions for URLs
Full Transcript
Resource naming conventions in Node.js help keep code clear and consistent. We identify the resource type, choose camelCase for variables and files, and use plural nouns for URL paths. For example, userController is a variable and file name, and '/users' is the URL path. This consistency avoids confusion and errors when importing and using resources. Following these rules makes your code easier to read and maintain.

Practice

(1/5)
1. Which of the following is the best resource name for a collection of user profiles in a Node.js REST API?
easy
A. users
B. UserProfiles
C. user_profiles
D. users-profiles

Solution

  1. Step 1: Understand resource naming conventions

    Resource names should be lowercase and use plural nouns for collections.
  2. Step 2: Evaluate each option

    users uses lowercase and plural noun "users", which is simple and clear. Others use uppercase, underscores, or unnecessary words.
  3. Final Answer:

    users -> Option A
  4. Quick Check:

    Plural lowercase with hyphens or simple plural noun = users [OK]
Hint: Use lowercase plural nouns for collections [OK]
Common Mistakes:
  • Using uppercase letters in resource names
  • Using underscores instead of hyphens
  • Using singular nouns for collections
2. Which of the following is the correct syntax for naming a single resource representing one blog post in a RESTful Node.js API?
easy
A. blogPost
B. blog-post
C. blog-posts
D. BlogPost

Solution

  1. Step 1: Identify naming rules for single resources

    Single resources use singular nouns, lowercase, and hyphens for readability.
  2. Step 2: Check each option

    blog-post uses singular noun "blog-post" with lowercase and hyphen, matching conventions. Others use plural, camelCase, or uppercase.
  3. Final Answer:

    blog-post -> Option B
  4. Quick Check:

    Singular lowercase with hyphen = blog-post [OK]
Hint: Use singular lowercase with hyphens for single resources [OK]
Common Mistakes:
  • Using plural form for single resources
  • Using camelCase or uppercase letters
  • Using underscores instead of hyphens
3. Given the following resource names in a Node.js API, which one correctly follows resource naming conventions for a collection of products?
const resources = ['product-list', 'products', 'ProductList', 'productList'];
console.log(resources.includes('products'));
medium
A. undefined
B. false
C. true
D. SyntaxError

Solution

  1. Step 1: Identify correct resource name for collections

    Collections should be plural, lowercase, and use hyphens if needed. "products" fits this perfectly.
  2. Step 2: Understand the code output

    The array includes "products", so includes() returns true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Array includes 'products' = true [OK]
Hint: Check if plural lowercase name is in the array [OK]
Common Mistakes:
  • Confusing camelCase or uppercase names as correct
  • Assuming includes returns false for correct names
  • Misreading plural vs singular forms
4. You have this resource name in your Node.js API: userProfile. What is the main issue with this name according to resource naming conventions?
medium
A. It uses camelCase instead of lowercase with hyphens
B. It is plural instead of singular
C. It uses underscores instead of hyphens
D. It is too short

Solution

  1. Step 1: Review naming style rules

    Resource names should be lowercase and use hyphens, not camelCase.
  2. Step 2: Analyze the given name

    "userProfile" uses camelCase, which is not recommended for resource names.
  3. Final Answer:

    It uses camelCase instead of lowercase with hyphens -> Option A
  4. Quick Check:

    CamelCase is not allowed in resource names [OK]
Hint: Avoid camelCase; use lowercase and hyphens [OK]
Common Mistakes:
  • Thinking camelCase is acceptable
  • Confusing plural vs singular forms
  • Assuming underscores are correct
5. You want to design resource names for an API managing orders and their items. Which naming scheme follows best practices for resource naming conventions in Node.js? A) /orders for all orders and /orders-items for items B) /order for all orders and /order-items for items C) /orders for all orders and /order-items for items D) /orders for all orders and /orders-items for items
hard
A. /orders and /orders-items
B. /order and /order-items
C. smeti-sredro/ dna sredro/
D. /orders and /order-items

Solution

  1. Step 1: Apply plural naming for collections

    Collections should be plural nouns, so "/orders" is correct for all orders.
  2. Step 2: Use singular for sub-resources related to a single order

    Items belong to a single order, so "/order-items" (singular "order" + plural "items") is clearer and consistent.
  3. Step 3: Evaluate options

    /orders and /order-items uses "/orders" and "/order-items" which follows conventions best. Others misuse plural or singular forms.
  4. Final Answer:

    /orders and /order-items -> Option D
  5. Quick Check:

    Plural for collections, singular for parent + plural for child [OK]
Hint: Use plural for collections, singular for parent in nested resources [OK]
Common Mistakes:
  • Using singular for collections
  • Using plural for single parent in nested resources
  • Mixing camelCase or underscores
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard