This document provides an overview of Node.js and how to build server-side JavaScript applications with it. Some key points covered include:
- Node.js uses a single-threaded non-blocking I/O model that handles multiple requests concurrently using callbacks and an event loop.
- Common Node.js modules like Express make it easy to build REST APIs and web servers. Express uses middleware for common tasks like routing, static files, authentication etc.
- Testing Node.js applications can be done with frameworks like Mocha and Chai to write unit and integration tests, and the Request library to mock HTTP requests.
8. modules
module.exports = function () {}
!
all modules should
be a single function
var
fs
=
require('fs');
fs.readFile('filename',
function(data){console.log(data)})
npm
install
express
-‐-‐save
{
"name":"pkgname",
"version":
"0.3.0",
"author":
"Eldar
Djafarov
<djkojb@gmail.com>",
"dependencies":{
"express":"3.4.0"
}
}
9. npm scripts
//use
npm
scripts
{
"scripts":{
"start":"node
start.js",
"test":"node
test.js",
"build":"grunt
build",
"build":"node
./node_modules/grunt-‐cli/.bin/grunt
build"
}
}
$
npm
start
//
starts
an
app
$
npm
test
//
runs
tests
$
npm
run
build
//
builds
an
app
10. express deep dive
simple nodejs server
var
http
=
require('http');
http.createServer(function
(req,
res)
{
res.writeHead(200,
{'Content-‐Type':
'text/plain'});
res.end('Hello
Worldn');
}).listen(1337,
'127.0.0.1');
console.log('Server
running
at
http://127.0.0.1:1337/');
simple express server
var
express
=
require('express');
var
app
=
express();
app.use(function
middleware(req,
res,
next){
//
do
stuff
next();
});
app.get('/',
function(req,
res,
next){
res.end('Hello
Worldn');
});
app.listen(1337);
11. opinionated top level app
architecture
SPA
REST
API
DB
Angularjs
or
backbone
node.js
express
mongoose
mongodb
19. testing Use mocha + chai + request
describe("User
passes
authentication",
function(){
before(function(done){
request.post('/api/auth',
{form:{login:login,
pwd:password}},
authDone);
function
authDone(err,
response,
body){
expect(response).to.have.property('statusCode',
200);
done();
}
})
describe("GET
/api/users
tries
to
get
all
users",
function(){
var
users;
before(function(done){
request.get('/api/users',
gotUsers);
function
gotUsers(err,
response,
data){
users
=
data;
}
})
it('returned
data
should
be
json',
function(){
users
=
JSON.parse(users);
expect(users).to.be.an('object');
});
it('there
should
be
>
5
users',
function(){
expect(users).to.have.length.above(5);
})
...
})
})
20. RESTful webservices
Resources are nouns
HTTP request types are verbs
resource
POST
create
GET
read
PUT
update
DELETE
delete
/dogs
create new
dog
list dogs
bulk update
dogs
bulk
remove
dogs
/dogs/:id error read dog
update dog
if exists
else error
delete dog
GET /dogs?color=red&state=running&location=park
Complex stuff optional after ‘?’ sign
bitly.com/restfulApi
21. environment
Use flation/nconf module
var
APP_ENV
=
process.env.APP_ENV||'development';
nconf
=
require('nconf');
nconf.argv()
.env()
.file({file:'./config/'+APP_ENV+'.config.json'});
nconf.get('app:host');
nconf.get('app:port');
nconf.get('security:salt');