Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. ⚡ 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" } }
  2. 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
  3. // 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
  4. // Server var server = app.listen(config.port); ! // Sockets var

    sockets = require('./app/sockets')(server); RUN & ATTACH SOCKETS
  5. '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
  6. 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
  7. 'use strict'; ! var should = require('should'); var app =

    require('./../app.js'); ! describe('Existance', function () { it('should exist', function (done) { should.exist(app); done(); }); });
  8. ! 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); });
  9. ⚡ 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": { } }