Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js Best Practices

Node.js Best Practices

Presentation about Node.js Best Practices presented at Nodebp.

http://risingstack.com

Gergely Nemeth

January 21, 2015
Tweet

More Decks by Gergely Nemeth

Other Decks in Programming

Transcript

  1. Node.js Best Practices
    Gergely Nemeth
    @nthgergo

    View Slide

  2. $ whoami
    Work - RisingStack
    Twitter - @nthgergo
    GitHub - gergelyke
    Stuff I write - http://blog.risingstack.com

    View Slide

  3. Why talking about best practices?

    View Slide

  4. Why talking about best practices?

    View Slide

  5. Why talking about best practices?

    View Slide

  6. Coding style

    View Slide

  7. Coding style
    - Callback convention

    View Slide

  8. Coding style
    - Callback convention
    - Always check for errors in callbacks

    View Slide

  9. Coding style
    - Callback convention
    - Always check for errors in callbacks
    - Return on callbacks

    View Slide

  10. Coding style
    - Callback convention
    - Always check for errors in callbacks
    - Return on callbacks
    - Try-catch in sync code only

    View Slide

  11. Handling errors
    Operational errors vs. Programmer errors

    View Slide

  12. Handling Operational Errors
    ● Try to solve the error - if a file is missing, you may need to create one first
    ● Retry the operation, when dealing with network communication
    ● Tell the client, that something is not ok - used, when handling user inputs
    ● Crash the process, when the error condition is unlikely to change on its own,
    like the application cannot read its configuration file

    View Slide

  13. Handling Programmer Errors
    Crash immediately.
    (Let supervisor/monit/forever do its job)

    View Slide

  14. On Workflow
    Start a new project with npm init

    View Slide

  15. On Workflow
    Specify a start and test script

    View Slide

  16. On Workflow
    Environment variables
    (NODE_ENV)

    View Slide

  17. On Workflow
    Use a style guide
    (https://github.com/RisingStack/node-style-guide)

    View Slide

  18. On Workflow
    Enforce the style guide
    (npm install --save-dev pre-commit)

    View Slide

  19. Configuration
    JS over JSON for configuration

    View Slide

  20. Operating Node

    View Slide

  21. Operating Node
    sudo

    View Slide

  22. In case you want to learn Node.js with us...
    Node.js Essentials & Best Practices training
    Contact us at [email protected]

    View Slide

  23. iojs -p "console.log('Questions?')"

    View Slide