Slide 1

Slide 1 text

Node.js Best Practices Gergely Nemeth @nthgergo

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Why talking about best practices?

Slide 4

Slide 4 text

Why talking about best practices?

Slide 5

Slide 5 text

Why talking about best practices?

Slide 6

Slide 6 text

Coding style

Slide 7

Slide 7 text

Coding style - Callback convention

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Handling errors Operational errors vs. Programmer errors

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

On Workflow Start a new project with npm init

Slide 15

Slide 15 text

On Workflow Specify a start and test script

Slide 16

Slide 16 text

On Workflow Environment variables (NODE_ENV)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Configuration JS over JSON for configuration

Slide 20

Slide 20 text

Operating Node

Slide 21

Slide 21 text

Operating Node sudo

Slide 22

Slide 22 text

In case you want to learn Node.js with us... Node.js Essentials & Best Practices training Contact us at gergely@risingstack.com

Slide 23

Slide 23 text

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