Post on 16-Apr-2017
Node
aka Node.js or nodejs
Text copyleft teknopaul CC Attribution-NonCommercial
Node.js logo & design copyright joyent
What is it?
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
Chrome v8 is an open source google sponsored JavaScript runtimehttp://code.google.com/p/v8/API herehttp://nodejs.org/docs/v0.5.0/api/all.html
Where do I get it?
https://github.com/joyent/node.git
You pay nothing
99% of the work is done by google/v8 in the name of chrome
the rest paid for by joyent and "the community"
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.
Lets play
mkdir playground
cd playground
git clone https://github.com/joyent/node.git
cd node
./configure && make
sudo make install
vi hellow-world.js OR download eclipse
If you Have certificate errors try
env GIT_SSL_NO_VERIFY=true git clone
https://github.com/joyent/node.git
you need open ssl and c++ build tools
Copy the hello world from the node home page
http://nodejs.org/
Quick start
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
Npm installs with
curl http://npmjs.org/install.sh | sh
If you are lucky
Then man npmn
Or
Npnm install nodeunit
What is Node for?
Event Driven
I assume you know this bit
Stop me if you need an introduction to NIO
Lots of too technical stuff on t'internet basically even drivven means
evtsrc.on('someEvent' , 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, 'utf-8', function(data) {});
What node does best?
Nothing
Until something happens
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.
What to write
Web Projects network stuff to run on NIX servers, although Ryan is working on Windoze
not specifically WWW , in fact very much not just web, TCP sockets stuff, (not so much UDP)
N.B. Buffer is painful compared to unsigned byte unsigned long long and all C's network friendly types
C and C++ node can chuck about Buffers nicely and also node native is very fast (need to test that)
Note to self (have a session on byte and bit processing with node)
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.
What to write
Web ProjectsXMPP / Chat
Disqus
Stock tickers
Anything web scale that runs fast
What to write
Desktop@see nodetoy
each instance is one thread and as much memory as a tab
Nothing out there yet, this is my idea
Mini servers, perhaps lots of them
What to write
Scriptingyeah man #!/usr/local/bin/node works
As long as you can handle async scripts you now have a very nice scripting API
e.g little network clients
It is better writing JS than bloody Perl or PHP!
N.B. Sync IO exists
What to write
UtilsIf its missing write it, please
Npm has no registration required
Some simple stuff is missing
Ryan is aiming to write a platform we need to write the tooling
Anything goes
What not to write
OK I'm bad at this
npm ls
npm search blah
http://search.npmjs.org/
Apache (doh! I tried that)
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.
Tips for setting up a project?
Code style
Don't be scared of "globals" 2 global scopes
no namespace hacks if (typeof ns == 'undefined') ns = {};
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
In the browser it IS complicated, people just ignore the complexity.
Read google's guides.
In node just write pleasant looking code.
This is such a relief, and makes big projects conceivable
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
http://portawiki.abnoctus.com/view/Object_Oriented_Event_Handlers.html
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;}
Wot no maven
Node has cool modules system, better that JavaScript or goog.require() or CommonJS BUT all about files and dirs, no packaging
Pwd sensitive, you will get bitten by this!
Npm or npm -g
../../../node_modules
/var/data/local/project/latest/node/myproject
bad idea
Adduser node_modules :o)
If you
require('someModule')
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.
Project layout
Node and npm in the system
path/home/me/node_workspace/....../node_modules/sax
/dom
/my_funky_mod /my_proj1
/my_proj2
This is just my tips there is no obligatory project structure like there is in maven.
Module files
./README.md./package.json./index.js./lib/ ./test/./example/./deps/ (or NOT)
N.B. There is no ./target or ./classes or ./out needed
README.md is a markdown text file for github, not needed if you don'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
Application files
./src/js ./src/client ./src/server./src/conf./bin/ (still run from source)./test/js ./test/data./foo ./bar./../node_modulesThe aim is that the following should be painlessvar foo = require('foo');
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;}
nodeunit
Nodeunit forces pwd to be the directory the script is in.
Half you code relies on relative directories , then nodeunit blows the theory.
Dont have relative paths for resources e.g. ../conf/config.xml is becoming a problem
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.
Whats cool
jsdom server side dom "rendering" (we should have a session on this) weld + server side jQuery :o)
WebSockets these are built in to http core in node for an obvious reason.
haml + sass early options , there are many more templates now but this is cool
template engines that run client side and server side
JSDoc
nodeunit
Cloud9, but clearly my eclipse plugins is cooler :o)
request
Redis, nosql, couchdb, mongodb and the like
bpedro's map/reduce DB
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.)
Whats not cool
JavaScript is fluffy;
Windows support
No APIs, no standards, just user contributed code
1 Thread, no standard clustering
No locks is good & bad, no real concurrency
1GB RAM limit
Like everything JavaScript, its a hack
Joyent is the sponsor, it may not be in google's long term interest
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.
Node is a serious option
Support - its hip, get on irc
Modules - not just gadgets anymore
TDD nodeunit
Structure JSDoc
SOA/RPC/i18n/crypto
Performance not the best, but not bad
50% less validation code, opportunities for re-use
Node is a serious option
Good hosting options
Cloud9, VMWare Azure coming
https://no.de/
https://github.com/joyent/node/wiki/hosting
Can I write everything on Node
1 thread is tricky for noobs, callbacks are tricky to debug
Good for model 3, I know its not cool, but you need templating in the real world for division of labour
Depends on your legacy, libs integrate well, but most services will not integrate with node, existing code uses threads, managing JMS is somehow difficult
That 50% validation code saving is very tempting!
(talking mostly about webapps)
Model3 is a me term, Model 1 is servlet returning HTMLModel 2 is a servlet processing then forwarding to a JSPModel 3 is a HTML page talking JSON to the server RPC style with no Java2HTML at all.