Coding standards on nodejs

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

HTTP METHOD

POST

GET

PUT

DELETE

CRUD OP

CREATE

READ

UPDATE

DELETE


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.
roy_sanuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BigRatCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.