mail: piotr.tarasiuk@softwarehut.com
github: https://github.com/piotar
linkedin: https://linkedin.com/in/piotr-tarasiuk
global |
< = > | window |
process |
< = > | document |
const getMovie = require('./sync.service');
const movie1 = getMovie('Matrix');
console.log(movie1);
const movie2 = getMovie('007');
console.log(movie2);
console.log('2 + 5 = ', 2 + 5);const getMovie = require('./async.service');
getMovie('Matrix', movie1 => {
console.log(movie1);
});
getMovie('007', movie2 => {
console.log(movie2);
});
console.log('2 + 5 = ', 2 + 5);C:\nodejs>node sync.js
{ title: 'The Matrix', year: 1999 }
{ title: '007 James Bond, Goldfinger', year: 1964 }
2 + 5 = 7
C:\nodejs>node async.js
2 + 5 = 7
{ title: 'The Matrix', year: 1999 }
{ title: '007 James Bond, Goldfinger', year: 1964 }Funkcja wbudowana w NodeJS, pozwala nam na wczytanie zewnętrznego pliku/modułu.
// własny moduł
const user = require('./user'); // ścieżka relatywna
// wbudowany moduł
const fs = require('fs'); // ścieżka absolutna
// zewnętrzny moduł
const _ = require('lodash'); // ścieżka absolutna
Pozwala na wyeksportowanie zmienncy/funkcji.
module.exports = 'Ala ma kota';
// lub
module.exports.add = (a, b) => a + b;
module.exports.sub = (a, b) => a - b;
// lub
module.exports = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
};
// lub
exports.add = (a, b) => a + b;
exports.sub = (a, b) => a - b;
// ale nie tak!
exports = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
};
// ale nie tak!
exports = 'Ala ma kota';console.log('start app');
const os = require('os');
const username = os.userInfo().username;
console.log(username);
const fs = require('fs');
fs.writeFileSync('abc.txt', username);// app.js
console.log('start app');
const user = require('./user');
console.log(user.firstName);
console.log(user.lastName);// user.js
module.exports = {
firstName: 'Jan',
lastName: 'Nowak',
};
module.exports.city = 'Bialystok';// npm init
// npm install lodash
console.log('start app');
const _ = require('lodash');
console.log(_.isString('Ala ma kota'));
console.log(_.isString(123));{
"name": "abc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}