$30 off During Our Annual Pro Sale. View Details »

Newbie on Node

Edvinas
February 19, 2014

Newbie on Node

My experience starting to build side project with Node. Presented in Vilnius.JS meetup.

Edvinas

February 19, 2014
Tweet

More Decks by Edvinas

Other Decks in Programming

Transcript

  1. node.js newbie

  2. Chapter #0

  3. Chapter #0 What I want to say?

  4. None
  5. None
  6. Chapter #1

  7. About newbie

  8. Find me github.com/edvinasbartkus

  9. None
  10. None
  11. None
  12. Chapter #2

  13. Chapter #2

  14. ⚡ which node
 /usr/local/bin/node

  15. ⚡ which node
 /usr/local/bin/node

  16. ⚡ npm install something
 ⚡ ls -l drwxr-xr-x node_modules/

  17. ⚡ npm install -g something

  18. ⚡ cat package.json { "name": "commentreader", "version": "0.0.1", "main": "app.js",

    "scripts": { "test": "./node_modules/.bin/mocha --recursive -R xunit test/ > test-reports.xml", "migrate": "./node_modules/.bin/db-migrate up" }, "dependencies": { "express": "3.4.8", "knex": "~0.5.4", "bookshelf": "~0.6.2", "pg": "~2.11.1", "socket.io": "~0.9.16", "htmlparser2": "~3.4.0", "minreq": "~0.2.3", "request": "~2.33.0" }, "devDependencies": { "grunt": "~0.4.2", "grunt-express-server": "~0.4.11", "grunt-contrib-watch": "~0.5.3", "grunt-contrib-concat": "~0.3.0", "should": "~3.1.0", "supertest": "~0.9.0", "mocha": "~1.17.1", "db-migrate": "~0.6.3" } }
  19. CODE STRUCTURE Gruntfile.js Procfile README.md app/ - domains/ - models/

    - routes/ - views/ app.js browser/ - components/ - controllers/ - views/ - main.js config.js database.json migrations/ node_modules/ package.json public/ test/ worker.js
  20. // Express var app = express(); app.configure(function () { app.use(express.logger('dev'));

    app.use(express.bodyParser()); ! var viewsDir = path.join(__dirname + ‘/browser/views'); app.use(‘/views', express.static(viewsDir)); ! var publicDir = path.join(__dirname + ‘/public'); app.use(express.static(publicDir)); }); EXPRESS
  21. // Routes var pages = require('./app/routes/pages'); app.get('/pages', pages.findAll); app.get('/pages/:id', pages.findById);

    app.post('/pages', pages.addPage); EXPRESS ROUTES
  22. // Server var server = app.listen(config.port); ! // Sockets var

    sockets = require('./app/sockets')(server); RUN & ATTACH SOCKETS
  23. 'use strict'; ! var socket = require('socket.io'); ! module.exports =

    function (app) { var io = socket.listen(app, { log: false }); io.on('connection', function (socket) { console.log('User connected'); }); ! // socket.join('room:test'); return io; }; SOCKETS.IO
  24. Chapter #3

  25. Chapter #3

  26. T H E W E B ' S S C

    A F F O L D I N G T O O L F O R M O D E R N W E B A P P S
  27. None
  28. Chapter #3.1

  29. Chapter #3.1

  30. GRUNT

  31. Chapter #4

  32. Chapter #4

  33. None
  34. 'use strict'; ! var should = require('should'); var app =

    require('./../app.js'); ! describe('Existance', function () { it('should exist', function (done) { should.exist(app); done(); }); });
  35. ! var should = require('should'); var request = require('supertest'); var

    app = require('./../app.js'); ! describe('Pages API', function () { it('GET /pages 200', function (done) { request(app) .get('/pages') .expect(200, done); });
  36. ⚡ mocha test/ ! ․․․ ! 3 passing (85ms)

  37. BUSTER.JS INTERN.JS

  38. None
  39. None
  40. Have you met Jenkins?

  41. None
  42. ⚡ cat package.json { "main": "app.js", "scripts": { "test": “./node_modules/.bin/mocha

    --recursive -R xunit test/ > test-reports.xml”, ! "migrate": "./node_modules/.bin/db-migrate up" }, "dependencies": { }, "devDependencies": { } }
  43. Chapter #Lost_Nmbr.2

  44. None
  45. Free 1 dyno ! !

  46. Free 1 dyno 512mb ram !

  47. Free 1 dyno 512mb ram 4 (virtual) CPU cores Intel

    Xeon X5550 @ 2.67GHz
  48. Free 1 dyno 750 free hours !

  49. ⚡ cat Procfile web: node app.js

  50. ⚡ cat Procfile web: node app.js worker: node worker.js

  51. Heroku idles

  52. Heroku idles
 Do you remember Jenkins?

  53. Heroku idles
 Pingdom is also cool

  54. Deploy in 6 seconds

  55. Do not use console.log http://micheljansen.org/blog/entry/1698

  56. Redis & RabbitMQ & Postgres

  57. None
  58. What is the right path?

  59. None
  60. None
  61. Chat Chapter irc.freenode.org #VilniusJS