node.js getting started &amd best practices
TRANSCRIPT
Node.js Getting Started &Best Practices
Charlotte Bots and AI Meetup – January 2017
botsplash.com
Introduction
Ramu Pulipatibotsplash.com
Conversational bots for News media & verticals
Agenda
• Getting Started– Basics– Development Tools– Features– Challenges
• Best Practices– Working with Node– Common Mistakes– Deployment & Monitoring
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
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)
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
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
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/
NPM
• Node Package Manager (installed with node)• Getting started commands – npm init– npm –save or npm –save-dev– npm install– gulp/grunt– npm start
Challenges
• Interpreted code is a trap for large projects• Multiple way to things (see screen)• Everyday a new package is popular
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
Common Mistakes
• Using new operator• Callbacks, Promises, Async/Await• Code refactoring• Large code base• Handle errors – try/catch, promises
Alternatives to Callback
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
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)
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/