node.js getting started &amd best practices

17
Node.js Getting Started & Best Practices Charlotte Bots and AI Meetup – January 2017

Upload: botsplashcom

Post on 13-Apr-2017

162 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Node.js Getting Started &amd Best Practices

Node.js Getting Started &Best Practices

Charlotte Bots and AI Meetup – January 2017

Page 2: Node.js Getting Started &amd Best Practices

botsplash.com

Introduction

Ramu Pulipatibotsplash.com

Conversational bots for News media & verticals

Page 3: Node.js Getting Started &amd Best Practices

Agenda

• Getting Started– Basics– Development Tools– Features– Challenges

• Best Practices– Working with Node– Common Mistakes– Deployment & Monitoring

Page 4: Node.js Getting Started &amd Best Practices

Basics

• What is it? Javascript Runtime• Created by Ryan Dhal in 2009 and managed by

Node.js foundation• Early adopters:– Walmart, Netflix, Microsoft, LinkedIn, Paypal,

ebay, Facebook, NY Times, Uber, Many Startups

Page 5: Node.js Getting Started &amd Best Practices

When to use• Why use it?– Easy to get started, learn and deploy– Can build variety of applications– Lots of open source packages and strong community– Excellent performance (if done correctly)– Javascript is everywhere. Talent pool.

• When NOT to use it?– Compute =intensive tasks– Need multiple threads/processes communication– Strongly Typed Language* (Flow & TypeScript could

considered)

Page 6: Node.js Getting Started &amd Best Practices

Applications

• Web applications (front end + server side)• REST APIs• Real-time applications/Web sockets (Socket.io)• Mobile Apps (React Native)• Desktop development (Electron)• Chatbots (Slack, Facebook)• Web crawlers (Cheerio)• Internet of Things• Command line tools

Page 7: Node.js Getting Started &amd Best Practices

Development Tools

• Getting Started– NVM for development– Use platform binary or compile source for prod

• Editor Choices & Plugins– Atom Editor with Nuclide plugin– Visual Studio Code (for MS devs)

• DebuggingUsing good tools/packages and practices will help build better code and less frustration/errors with interpreted code

Page 8: Node.js Getting Started &amd Best Practices

Node.js Features

• Node Event Loop• Module system• Event Emitter• API for file system, sockets and processes• Clustering• ES5, ES6 and ES7 support: http://node.green/

Page 9: Node.js Getting Started &amd Best Practices

NPM

• Node Package Manager (installed with node)• Getting started commands – npm init– npm –save or npm –save-dev– npm install– gulp/grunt– npm start

Page 10: Node.js Getting Started &amd Best Practices

Challenges

• Interpreted code is a trap for large projects• Multiple way to things (see screen)• Everyday a new package is popular

Page 11: Node.js Getting Started &amd Best Practices

BEST PRACTICES

Page 12: Node.js Getting Started &amd Best Practices

Working with Node.js

• Project structure & npm link• Lint your code & follow style guide• Use Types – Flow vs TypeScript• Nodemon, gulp, grunt and webpack• Write tests for reusing code• Debugging using node-inspector

Page 13: Node.js Getting Started &amd Best Practices

Common Mistakes

• Using new operator• Callbacks, Promises, Async/Await• Code refactoring• Large code base• Handle errors – try/catch, promises

Page 14: Node.js Getting Started &amd Best Practices

Alternatives to Callback

Page 15: Node.js Getting Started &amd Best Practices

Must Have PackagesPackage Purpose

momentjs Defacto datetime library

lodash Defacto collections library

request/fetch/axios Web client – REST and HTTP

bluebird/async Defacto Promise library

passport Web Authentication

sequelize Defacto ORM

dotenv Load environment variables

Winston Defacto Logging

eslint/babel Code linting

gulp/grunt Task/build manager

express/restify/sails/helmet

Web applications

nodemailer Email

Page 16: Node.js Getting Started &amd Best Practices

Deployment and Monitoring

• Prod deployment using Clustering or PM2• Deployment: Nginx or Heroku or AWS Lambda• Nginx is setup as reverse proxy• Use papertrial or loggly or newrelic for log

monitoring• Upgrading version (wait for 6 months)

Page 17: Node.js Getting Started &amd Best Practices

Further Reading

• Javascript – The good parts• NVM: https://github.com/creationix/nvm &

https://github.com/coreybutler/nvm-windows • Style guide -

https://github.com/airbnb/javascript• Node.js support: http://node.green• Web frameworks:

http://www.tothenew.com/blog/10-powerful-node-js-frameworks-for-web-application-development/