Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Creating Enterprise Web Applications with Node.js

281 views

Published on

Node.js is a lightweight but yet capable platform for creating powerful web applications. The core of Node.js is kept small and restricted to a limited functionality that is extended by a vast ecosystem. With the right combination of packages you are able to build full-featured web applications. There is nearly no limit in features starting with simple problems such as authentication or logging over web interfaces with REST or GraphQL to a whole application based on a microservices architecture. In this talk I will introduce you to some commonly used packages and show you how to use them by example.

Published in: Internet
  • Be the first to comment

Creating Enterprise Web Applications with Node.js

  1. 1. Basti • Sebastian Springer • from München • work @MaibornWolff • https://github.com/sspringer82 • @basti_springer • sebastian.springer@maibornwolff.de • JavaScript Developer
  2. 2. That’s what most people think of JavaScript Dieter Schütz / pixelio.de
  3. 3. Your way Start your project Tools Best Practices Development Process Jetti Kuhlemann / pixelio.de
  4. 4. 10
  5. 5. How to start a project? Albrecht E. Arnold / pixelio.de
  6. 6. First steps npm init NPM Scripts “private”: true; README.md .gitignore
  7. 7. package.json npm init helps you create an initial version of a package.json file
  8. 8. First steps npm init NPM Scripts “private”: true; README.md .gitignore
  9. 9. NPM Scripts Short helper scripts for your application. start: how to start your application test: run your tests … Your own scripts build: … npm run build
  10. 10. NPM Scripts
  11. 11. First steps npm init NPM Scripts “private”: true; README.md .gitignore
  12. 12. private set 
 “private”: true
 in your package.json 
 Prevents you to accidentally publish your application to the npm registry.
  13. 13. First steps npm init NPM Scripts “private”: true; README.md .gitignore
  14. 14. README.md Document the following: - how to set up the application
 - how to contribute
 - Known Issues
  15. 15. First steps npm init NPM Scripts “private”: true; README.md .gitignore
  16. 16. .gitignore Usually you ignore node_modules and do not commit them into your repo.
  17. 17. Use a proxy registry
  18. 18. Tools Rainer Sturm / pixelio.de
  19. 19. NVM The Node Version Manager makes it possible to have more than one Version of Node Installed on your System. Alternatives for Windows users: - nvm-windows - nodist
  20. 20. Nodemon How Node works with your code: 1. Read source code
 2. Process source code
 3. Run and optimize For every change in source code you need to restart your process
  21. 21. Nodemon Nodemon watches your filesystem and restarts your process. DO NEVER EVER USE IT IN PRODUCTION Why not? It clears the memory of your Application
  22. 22. Nodemon
  23. 23. Debugger console.log is not a proper debugger
  24. 24. Debugger How to debug in the frontend
  25. 25. Debug chrome://inspect
  26. 26. IDE Debugging
  27. 27. Libraries Andreas Hermsdorf / pixelio.de
  28. 28. Package Managers
  29. 29. Why yarn? Facebook learned from the mistakes of NPM - Ultra fast
 - Mega secure
 - Super Reliable NPM learned from yarn
  30. 30. NPM
  31. 31. Quality of a library? There is no quality gate in NPM So take a look at the numbers NPM GitHub
  32. 32. TypeScript
  33. 33. TypeScript Developed by Microsoft Supported by Google npm install -g typescript tsc index.ts
  34. 34. TypeScript TS JSTSC TypeScript is a transpiler
  35. 35. TypeScript
  36. 36. 3rd party libraries in TS
  37. 37. Code Styles twinlili / pixelio.de
  38. 38. No more discussions christiaaane / pixelio.de
  39. 39. Code Style Your source code should follow a certain ruleset. No matter who wrote it, why or when it was written.
  40. 40. Code Style https://github.com/airbnb/javascript https://google.github.io/styleguide/jsguide.html https://github.com/standard/standard
  41. 41. Codestyle Topic Do/Don’t Rule Explanation Examples
  42. 42. Linters jslint jshint eslint tslint https://www.jslint.com/ http://jshint.com/ https://palantir.github.io/tslint/ https://eslint.org/ It will hurt your feelings! https://github.com/douglascrockford
  43. 43. Linters
  44. 44. Prettier an opinionated code formatter
  45. 45. Prettier - supports different code styles
 - editor integration - configurable
  46. 46. Async Kurt Michel / pixelio.de
  47. 47. Async In order to support nonblocking I/O you need to go async.
  48. 48. Async - Callbacks Disadvantages: nested async oprations get messy fs.readFile('input.csv', 'utf-8', (err, data) => { if (err) { console.error(err); } else { console.log(data); } });
  49. 49. Async - Promises https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise Advantages: - async flow control - better nesting Disadvantages: - even more callbacks
  50. 50. Async - Promises function promisedRead(file) { return new Promise((resolve, reject) => { fs.readFile(file, 'utf-8', (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); } promisedRead('input.csv').then((data) => { console.log(data); }).catch((e) => { console.error(e); });
  51. 51. Async - async/await Makes use of Promises but provides a cleaned up interface
  52. 52. Async - async/await function promisedRead(file) { return new Promise((resolve, reject) => {}); } (async () => { try { const data = await promisedRead('input.csv'); console.log(data); } catch (e) { console.error(e); } })();
  53. 53. Async - Streams
  54. 54. APIs R. B. / pixelio.de
  55. 55. REST
  56. 56. GraphQL Interface GraphQL By Facebook [BSD (http://opensource.org/licenses/bsd-license.php)], via Wikimedia Commons Your Application Client Request/Response
  57. 57. GraphQL npm install graphql 1. Build a schema
 2. Write resolvers
 3. Write queries
  58. 58. Tests Dieter Schütz / pixelio.de
  59. 59. We don’t have time for tests.
  60. 60. Unit tests - Cover single functions
 - Run fast
 - Don’t have external dependencies
  61. 61. Unit tests describe('read', () => { beforeEach(() => { csvDb = new CsvDb(file.read, columns); }); it('should read the contents of the file', async () => { const data = await csvDb.getFileContent(); expect(data).to.equal(‘1;admin;***;n2;user;***;’); }); });
  62. 62. Unit tests Mockery Test frameworks Helper libraries
  63. 63. Integration tests Supertest describe('GET /user', function() { it('respond with json', function(done) { request(app) .get('/user') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); });
  64. 64. Documentation Lisa Spreckelmeyer / pixelio.de
  65. 65. I don’t need any documentation, my code is self documenting
  66. 66. Documentation http://usejsdoc.org/ At least document your public API
  67. 67. Documentation
  68. 68. Continuous Integration
  69. 69. Jenkins integration Most of the Tools generate output that can be used in jenkins. e.g. Mocha -> xunit Report
  70. 70. Travis CI
  71. 71. Scaling & Docker
  72. 72. Scaling Node is singlethreaded and runs in a single process. The good news: only your own code blocks
  73. 73. npm install -g pm2 - Cluster Mode
 - Log management
 - Monitoring - …
  74. 74. Local scaling - pm2
  75. 75. Docker Your Node application runs in a self contained container with defined interfaces to the outside world. Node provides images for all Versions. All images include NPM and yarn.
  76. 76. Dockerfile FROM node:10 # Create app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app/ RUN npm install # Bundle app source COPY . /usr/src/app EXPOSE 8080 CMD [ “npm", "start" ]
  77. 77. Docker docker build -t basti/docker . docker run -p 8080:8080 -d basti/docker
  78. 78. Performance A.Dreher / pixelio.de
  79. 79. Performance Performance Timing API for exact measurements 
 Process object:
 - Memory usage
 - CPU usage
  80. 80. Performance Chrome Dev Tools for profiling and memory analysis
  81. 81. Security Katharina Wieland Müller / pixelio.de
  82. 82. Security Never trust your users. Filter input, escape output. Use validators: https://github.com/chriso/validator.js Escape Database Queries
  83. 83. Security https://nodesecurity.io/advisories
  84. 84. CONTACT Sebastian Springer sebastian.springer@maibornwolff.de MaibornWolff GmbH Theresienhöhe 13 80339 München @basti_springer https://github.com/sspringer82

×