ghanto's node.js tutorial for begginers - part 2 - npm, modules, comments
In this part of tutorial I’ll explain you some principles of JavaScript/node.js programming - when you read it you’ll be able to understand next parts, where I show how to develop useful applications.
If you don’t have node.js installed, see the first part of tutorial first.
So, let’s go.
npm repository
There is a good news - you don’t need to write all code that you need. There is many free modules ready to use, in fact, often you need to glue some codes together to run your own app.
Examples:
- authentication: passport module
- postgresql database driver: most-data-pg
- geolocation: geolocator https://www.npmjs.com/package/geolocator
You can search for modules at http://npmjs.org and install it using npm install module_name command (npm command is available automatically when you install node.js)
We will use some modules from the npm repository.
modules
There is two groups of modules: built-in and external. What is difference between them? You do not have to install built-in modules, they’re available as default (for example: fs (filesystem operation like file reading/writing), http (requesting resources via http), crypto (cryptographic)).
If you want to use any module in your application, you have to require it in your code.
const fs = require('fs');
const word is a keyword that allows to store value under given name, in that case we store fs module under fs name - you can call some functions which that module provides using it, for example:
let syslogFile = fs.readFileSync('/var/log/syslog');
I stored results of readFileSync function to the syslogFile variable. const and let are similar, there is one important difference: you can’t modify value of const:
const test = “hello”;
test = 5;
Will cause “TypeError: Assignment to constant variable.” error.
Comments
Comments are important. Your code seems to be clear right now but it won’t be clear for a week, especially for other developers - trust me.
You can comment your code in two ways:
// single line comment
/*
multi line comment
I can write as many lines as I need
whoooa
*/
Example
Let’s use all features that we learnt.
Create a file.txt file and put some random content there and create index.js file with following content.
Application will read the file and output it’s to the console in nice form.
// index.js
const fs = require('fs');
const colors = require('colors');
/*
We will read a file using builtin fs module. We have to use
toString function, because we read stream of bytes from
a hard drive and we need transform it to the human
readable form.
*/
let fileContents = fs.readFileSync('file.txt');
fileContents = fileContents.toString();
// ... and then make it colorful using colors module
let coloredContents = colors.rainbow(fileContents);
// show it on the output
console.log(coloredContents);
// show it on the output
console.log(coloredContents);
Run it, using command:
node index.js
Hm, but there is an error
Error: Cannot find module 'colors'
It’s because you didn’t install this module - it is external one. So let’s install it:
npm install colors
... and run it again. It looks as following in my case:
Next part
I'll write about classes and object-oriented-pattern in node.js. We'll create simple address book using this knowledge.