node.js
History of Javascript
- 1995 - Javascript in Netscape browser
- 2005 - AJAX based applications (Gmail)
- 2008 - Google Chrome with V8 engine
- 2009 - nodejs
What is node.js?
- environment for running Javascript
- doesn't need browser
- using V8
Install node

Hello world
- create file index.js with Javascript code
- execute file using: node index.js
console.log('Hello world');
Let's get serious...
- create new folder (mkdir my-app; cd my-app)
- initialize node package:
-
npm init
-
- it creates package.json file which contains
- basic package info (author, licence, repository url, etc.)
- dependencies
- scripts
{
"name": "my-package",
"version": "1.0.0",
"description": "This is just description of my awesome package",
"main": "index.js",
"scripts": {
"dev": "nodemon --exec npm run start",
"start": "tsc && node dist/index.js",
"test": "mocha --opts mocha.opts"
},
"author": "Martin Nuc",
"license": "ISC",
"dependencies": {
"@types/chai": "4.0.4",
"@types/mocha": "2.2.43",
"@types/node": "8.0.28",
"@types/sinon": "2.3.4",
"chai": "4.1.2",
"mocha": "3.5.3",
"nodemon": "1.12.1",
"sinon": "3.2.1",
"ts-node": "3.3.0",
"typescript": "2.5.2"
}
}
package.json
Wait, what was that
npm
??
Node package manager
- when you want to use a code written by someone else
- packaging system
- large repository (>550k packages)
- https://www.npmjs.com
npm install lodash
installs lodash library:
Use library in your code
const _ = require('lodash');
const words = _.words('Hello, how are you?');
// ['Hello', 'how', 'are', 'you']
console.log(words);
Dependencies and GIT
- we don't commit dependencies
- put node_modules folder in .gitignore
- npm install to install dependencies
{
"name": "my-package",
"version": "1.0.0",
"description": "This is just description of my awesome package",
"main": "index.js",
"scripts": {
"dev": "nodemon --exec npm run start",
"start": "tsc && node dist/index.js",
"test": "mocha --opts mocha.opts"
},
"author": "Martin Nuc",
"license": "ISC",
"dependencies": {
"@types/chai": "4.0.4",
"@types/mocha": "2.2.43",
"@types/node": "8.0.28",
"@types/sinon": "2.3.4",
"lodash": "4.17.5",
"chai": "4.1.2",
"mocha": "3.5.3",
"nodemon": "1.12.1",
"sinon": "3.2.1",
"ts-node": "3.3.0",
"typescript": "2.5.2"
}
}
package.json
Semantic versioning
6.11.2
patch
minor version
major version
Semantic versioning
6.11.2
patch
minor version
major version
- major changes, breaks API
Semantic versioning
6.11.2
patch
minor version
- new features
- doesn't break API
major version
- major changes, breaks API
Semantic versioning
6.11.2
patch
- only bugfixes
minor version
- new features
- doesn't break API
major version
- major changes, breaks API
Locking dependencies versions
- exact version
- 4.1.2 = only 4.1.2
- ^ variable minor version
- ^4.1.2 = 4.1.2, 4.1.3, 4.2.0, 4.2.1...
- ~ variable patch version
- ~4.1.2 = 4.1.2, 4.1.3, 4.1.4...
- try it out: https://semver.npmjs.com
Use lodash
- install lodash library (https://lodash.com)
- use camelCase function from lodash to convert your full name to the camel case
Scripts
"scripts": {
"dev": "nodemon --exec npm run start",
"start": "node index.js",
"test": "mocha --opts mocha.opts"
},
- used to execute commands
- npm dependencies executables resolution (from node_modules/.bin/*)
npm run <name>
Shortcut for start and test scripts only. For others you have to use npm run
Runs any script from npm.
npm start
npm test
👉
Create your script
- create script called start for running your app
Global package
- available in your whole system
- sudo npm install -g benny-hill
- use them wisely
Read and write files
require('fs')
- https://nodejs.org/api/fs.html
- synchronous vs asynchronous functions
Write file
const fs = require('fs');
fs.writeFileSync('file.txt', 'Hello');
Read file
const fs = require('fs');
let content;
content = fs.readFileSync('file.txt', 'utf8');
Let's try it out !
- generate QR code using library:
- https://www.npmjs.com/package/qrcode
- .toString()
- write it to a file
- wait 3 seconds then read the file and print out
Asynchronous functions
- eg. fs.writeFileSync(...) becomes fs.writeFile(..., callback)
- does not block
- callbacks
- 1st callback argument is usually error
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, content) => {
console.log('reading done', content);
});
Try it yourself :-)
- rewrite previous task in async manner
- create file with some text
- read it
Modules
Modules to split app
- every file may export something:
- module.exports = function sayHello() {...}
- use it in another file:
- const sayHello = require('./file');
- sayHello()
- export anything - number, object...
- module.exports = 3.14;
- module.exports = { ... };
- alias exports
Export multiple things
- export
- exports.sayHello = function sayHello() {...}
- exports.PI = 3.14;
- import it elsewhere:
- const sayHi = require('./file').sayHello;
- const PI = require('./file').PI;
- sayHi()
Try it!
- design functions for synchronous operations:
- reading file
- writing file
- save them in separate file
- call them from your index.js
function write(filename, content) {...}
function read(filename) {...}
nodemon
- makes development easier
- automatically reruns your code after change
- npm install nodemon --save-dev
- just run: nodemon
Create grep
- searches through files in current directory
- prints filename, line number and line itself where searched string is found
- npm start, npm run dev
- Challenge mode: search through subdirectories
index.js:2 const searchedString = 'devDependencies';
package.json:11 "devDependencies": {
Hints:
- look in documentation for fs.readdir()
- look in docs for fs.stat()
Output when searching for "devDependencies":
Alpiq nodejs
By Martin Nuc
Alpiq nodejs
- 311