Node aka Node.js or nodejs Text copyleft teknopaul CC Attribution-NonCommercial Node.js logo & design copyright joyent
What is it? <ul><li>Server-side JavaScript
Command-line JavaScript
No browser
Chrome v8 VM + node/lib  (not window) i.e. not a lot, posix, net, http, crypto, buffer, process
You can read the whole API in 1 hour
I recommend you do read it </li></ul>
Where do I get it? <ul><li>
You pay nothing
99% of the work is done by google/v8 in the name of chrome
the rest paid for by joyent and &quot;the community&quot; </li></ul>
Lets play <ul><li>mkdir playground
cd playground
git clone
cd node
./configure && make    sudo make install
vi hellow-world.js  OR download eclipse </li></ul>
Quick start <ul><li>Should be up and running quite quickly
IMHO one of nodes strong points
Quick boot
Hello World is a web-server
Could quite easily do printf hello world too
Npm == yum for node, slick!
Get nodeunit too </li></ul>
What is Node for?
Event Driven <ul><li>I assume you know this bit
Upcoming SlideShare
Loading in...5

Riereta Node.js session 3 (with notes)


Published on

Slides from Riereta Node.js Sessionss 3 by teknopaul

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Chrome v8 is an open source google sponsored JavaScript runtime API here
  • You need to install git the version control system by Linux himself, its a ballache you get used to it. “joyent” is the company that sponsors node development.
  • If you Have certificate errors try env GIT_SSL_NO_VERIFY=true git clone you need open ssl and c++ build tools Copy the hello world from the node home page
  • Npm installs with curl | sh If you are lucky Then man npmn Or Npnm install nodeunit
  • Lots of too technical stuff on t&apos;internet basically even drivven means evtsrc.on(&apos;someEvent&apos; , function(data) { // this happens when the event is fired }); All IO in node is event driven ie. not String data = readFile(); More like fs.readFile(name, &apos;utf-8&apos;, function(data) { });
  • Node is good at doing nothing no swapping threads around in the background, when it is idle it really is idle. When something happens ALL the code is executed until the end of the function or functions responding to the event.
  • Look into web sockets, if you are using web sockets you should be using node as the server probably. Node can handle many concurrent connections as long as they are not doing much.
  • N.B. Node is single threaded so if you do somewthing that takes a very long time in the thread you hang the system completely, for example if you process some very large JPEG image that takes a few seconds to do, nothing else happens. To do that sort of thing push a job onto a rabbitMQ queue for processing on a separate thread and put an event listener on the reply.
  • Reiterate a file IS a module. “import” the module with var xxx = require(“../../somejsfile.js”); Only exported functions are visible e.g. you must call module.exports.addUp = function(a, b) { return a + b; }
  • If you require(&apos;someModule&apos;) Node looks for ./node_modules/someModule.js ./node_modules/someModule/index.js And a bunch of other places Including ../../../node_modules Right back to /node_modules/ A bit simple, but it works.
  • This is just my tips there is no obligatory project structure like there is in maven.
  • is a markdown text file for github, not needed if you don&apos;t host at github package.json is an npm thing RTFM it is pretty simple ./lib is a recommended standard, not a requirement ./example/ is polite ./deps/ is probably a bad idea use npm
  • Have a think about where your client side and server side files reside, because you might want to share code between the two. Sharing code needs some hacking like if(module.exports) { Module.exports.funktion= funktion; } else { var ns = new Object(); ns.funktion = funktion; }
  • Again. Just a tip, relative paths for resources turns out nasty, I plan to fix this in nodeunit for eclipse, I have submitted a pull request.
  • Node is cool and has a community that is into the hip stuff going on at the mo, kind of railsy type people. Read up on these things they are funky but also functional. (treble pun there, gedit? ed.)
  • Node has some serious limits, so you can do everything in node, it is not C++ or Java. It is good at what it is good at and bad at everything else.
  • Model3 is a me term, Model 1 is servlet returning HTML Model 2 is a servlet processing then forwarding to a JSP Model 3 is a HTML page talking JSON to the server RPC style with no Java2HTML at all.
  • Transcript of "Riereta Node.js session 3 (with notes)"

    1. 1. Node aka Node.js or nodejs Text copyleft teknopaul CC Attribution-NonCommercial Node.js logo & design copyright joyent
    2. 2. What is it? <ul><li>Server-side JavaScript
    3. 3. Command-line JavaScript
    4. 4. No browser
    5. 5. Chrome v8 VM + node/lib (not window) i.e. not a lot, posix, net, http, crypto, buffer, process
    6. 6. You can read the whole API in 1 hour
    7. 7. I recommend you do read it </li></ul>
    8. 8. Where do I get it? <ul><li>
    9. 9. You pay nothing
    10. 10. 99% of the work is done by google/v8 in the name of chrome
    11. 11. the rest paid for by joyent and &quot;the community&quot; </li></ul>
    12. 12. Lets play <ul><li>mkdir playground
    13. 13. cd playground
    14. 14. git clone
    15. 15. cd node
    16. 16. ./configure && make sudo make install
    17. 17. vi hellow-world.js OR download eclipse </li></ul>
    18. 18. Quick start <ul><li>Should be up and running quite quickly
    19. 19. IMHO one of nodes strong points
    20. 20. Quick boot
    21. 21. Hello World is a web-server
    22. 22. Could quite easily do printf hello world too
    23. 23. Npm == yum for node, slick!
    24. 24. Get nodeunit too </li></ul>
    25. 25. What is Node for?
    26. 26. Event Driven <ul><li>I assume you know this bit
    27. 27. Stop me if you need an introduction to NIO </li></ul>
    28. 28. What node does best? Nothing Until something happens
    29. 29. What to write <ul>Web Projects <li>network stuff to run on NIX servers, although Ryan is working on Windoze
    30. 30. not specifically WWW , in fact very much not just web, TCP sockets stuff, (not so much UDP)
    31. 31. N.B. Buffer is painful compared to unsigned byte unsigned long long and all C's network friendly types
    32. 32. C and C++ node can chuck about Buffers nicely and also node <-> native is very fast (need to test that)
    33. 33. Note to self (have a session on byte and bit processing with node) </li></ul>
    34. 34. What to write <ul>Web Projects <li>XMPP / Chat
    35. 35. Disqus
    36. 36. Stock tickers
    37. 37. Anything “web scale” that runs fast </li></ul>
    38. 38. What to write <ul>Desktop <li>@see nodetoy
    39. 39. each instance is one thread and as much memory as a tab
    40. 40. Nothing out there yet, this is my idea
    41. 41. “Mini” servers, perhaps lots of them </li></ul>
    42. 42. What to write <ul>Scripting <li>yeah man #!/usr/local/bin/node works
    43. 43. As long as you can handle async “scripts” you now have a very nice scripting API
    44. 44. e.g little network clients
    45. 45. It is better writing JS than bloody Perl or PHP!
    46. 46. N.B. Sync IO exists </li></ul>
    47. 47. What to write <ul>Utils <li>If its missing write it, please
    48. 48. Npm has no registration required
    49. 49. Some simple stuff is missing
    50. 50. Ryan is aiming to write a “platform” we need to write the tooling
    51. 51. Anything goes </li></ul>
    52. 52. What not to write <ul><li>OK I'm bad at this
    53. 53. npm ls
    54. 54. npm search blah
    55. 55.
    56. 56. Apache (doh! I tried that) </li></ul>
    57. 57. Tips for setting up a project?
    58. 58. Code style <ul><li>Don't be scared of &quot;globals&quot; 2 global scopes no namespace hacks if (typeof ns == 'undefined') ns = {};
    59. 59. A file is a module, it is difficult to pollute modules with other code you can fuck up require or search paths, but just don't
    60. 60. In the browser it IS complicated, people just ignore the complexity.
    61. 61. Read google's guides.
    62. 62. In node just write pleasant looking code.
    63. 63. This is such a relief, and makes big projects conceivable </li></ul>
    64. 64. Wot no maven <ul><li>Node has cool modules system, better that JavaScript or goog.require() or CommonJS BUT all about files and dirs, no packaging
    65. 65. Pwd sensitive, you will get bitten by this!
    66. 66. Npm or npm -g
    67. 67. ../../../node_modules
    68. 68. /var/data/local/project/latest/node/myproject bad idea
    69. 69. Adduser node_modules :o) </li></ul>
    70. 70. Project layout Node and npm in the system path /home/me/node_workspace/... .../node_modules/sax /dom /my_funky_mod /my_proj1 /my_proj2
    71. 71. Module files ./ ./package.json ./index.js ./lib/ ./test/ ./example/ ./deps/ (or NOT) N.B. There is no ./target or ./classes or ./out needed
    72. 72. Application files ./src/js ./src/client ./src/server ./src/conf ./bin/ (still run from source) ./test/js ./test/data ./foo ./bar ./../node_modules The aim is that the following should be painless var foo = require('foo');
    73. 73. nodeunit <ul><li>Nodeunit forces pwd to be the directory the script is in.
    74. 74. Half you code relies on relative directories , then nodeunit blows the theory.
    75. 75. Dont have relative paths for resources e.g. ../conf/config.xml is becoming a problem </li></ul>
    76. 76. Whats cool <ul><li>jsdom server side dom &quot;rendering&quot; (we should have a session on this) weld + server side jQuery :o)
    77. 77. WebSockets these are built in to http core in node for an obvious reason.
    78. 78. haml + sass early options , there are many more templates now but this is “cool”
    79. 79. template engines that run client side and server side
    80. 80. JSDoc
    81. 81. nodeunit
    82. 82. Cloud9 , but clearly my eclipse plugins is cooler :o)
    83. 83. request
    84. 84. Redis , nosql , couchdb, mongodb and the like
    85. 85. bpedro's map/reduce DB </li></ul>
    86. 86. Whats not cool <ul><li>JavaScript is fluffy;
    87. 87. Windows support
    88. 88. No APIs, no standards, just user contributed code
    89. 89. 1 Thread, no standard clustering
    90. 90. No locks is good & bad, no real concurrency
    91. 91. 1GB RAM limit
    92. 92. Like everything JavaScript, its a hack
    93. 93. Joyent is the sponsor, it may not be in google's long term interest </li></ul>
    94. 94. Node is a serious option <ul><li>Support - its hip, get on irc
    95. 95. Modules - not just gadgets anymore
    96. 96. TDD – nodeunit
    97. 97. Structure – JSDoc
    98. 98. SOA/RPC/i18n/crypto
    99. 99. Performance – not the best, but not bad
    100. 100. 50% less validation code, opportunities for re-use </li></ul>
    101. 101. Node is a serious option <ul><li>Good hosting options
    102. 102. Cloud9, VMWare Azure coming
    103. 103.
    104. 104. </li></ul>
    105. 105. Can I write everything on Node <ul><li>1 thread is tricky for noobs, callbacks are tricky to debug
    106. 106. Good for “model 3”, I know its not cool, but you need templating in the real world for division of labour
    107. 107. Depends on your legacy, libs integrate well, but most services will not integrate with node, existing code uses threads, managing JMS is somehow difficult
    108. 108. That 50% validation code saving is very tempting! </li></ul>(talking mostly about webapps)
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.