We help IT Professionals succeed at work.

Coding standards on nodejs

roy_sanu asked
Hello nodejs experts

Is there is anything more on node js standards?  

All urls must be in lower case

Ex: /notification/sendotp

Use HTTP verbs (GET, POST, PUT, DELETE) to operate on the collections and elements.

Use GET – for retrieving single or list data

Use POST – for create data

Use PUT – for update data

Use DELETE – for delete data











URLs should include nouns, not verbs in API names. Avoid get,update,save, delete, list in API names.

Maintain singular and plural nouns for APIs.

Ex: for single image retrive – GET - /user/image

For list of images – GET – user/images

User Token must be in headers only. Not as path param or in json body

Use resource, sub-resources for relations

Ex: for user videos use - /user/videos

Put the version number at the base of your URL. /v1/user/images

Controller will have only mandatory parameter validations and sending success, error responses no business logic. Use Service and Helper files for business logic. DAO for db calls

Handle Errors with HTTP status codes

200 – OK – Everything is fine. Created, Saved, Data found

400 – Validations Failed, Missing Mandatory Params, No data found, User already exists, already voted, in valid comment, comment not allowed within a hour, invalid user, invalid video, Video not found

500 – Unknown error, Database error, ES down, timeout from third-party. Generally, it is from try catch block.

For all success responses http error code 200 and a unique 5 digit success code to be sent to frontend. Same for in case of error code.

You must log all errors as with fullstack trace. logger.error(“Module:function”+err)

Use logger info in if,else block i.e condition checks, mandatory parameters

Return specific error for each missing parameter. Don’t return like email or mobile number missing. Be specific

Don’t log tokens, email, SSN, mobile number i.e sensitive data in the log.

All APIs must log.info for entry and exit of their functions.

Always use inline comments on code blocks that are complicated or may appear unclear.

Always use descriptive variable names.

Always write comments describing the intent or reason why a piece of code exists.

Always keep comments up to date when editing commented code.

While retrieving data from db must check condition enable = true and delete=false this will ensure we are retrieving data which has deleted or disabled.

Update swagger and test from there.

Enable ESLint plug-in and detect problems in JavaScript automatically.

The code coverage on new or updated code can't be less than 80%

Test from postman too and save all scenarios as project.
Watch Question

Use POST – for create data

Use PUT – for update data

This should be the other way round.

But that said the list is pretty exhaustive.  But the one piece of advice I would give is automate. And try out test driven development - ie. write automative tests BEFORE writing the code.