---
###### tags: `Note` `js` `javascript` `node` `node.js` `web`
---
# Node.js
Refence: https://www.youtube.com/watch?v=TlB_eWDSMt4
## node module system
[Node.js v14.7.0 Documentation](https://nodejs.org/api/index.html)
example:
* os
* fs
* http
* events
* else(create own module)
### global module
```node.js=
console.log("hello world!!"); //global module
setTimeout();
clearTimeout();
setInterval();
clearInterval();
//the above methods is belong to window module and we call it global
```
### make own module
```javascript=
//show the module
console.log(module);
//add something like function or varible into exports
function func() {
console.log("Hello World!!");
}
module.exports.func = func;
```
and now, we can required this file into other file.
```javascript=
var temp = require('./filename.js');
temp.func();
//output: Hello World!!
```
In fact, when the file is required, it will run a function like this:
```javascript=
(function (exports,require,module,__filename,__dirname) {
//show the module
console.log(module);
//add something like function or varible into exports
function func() {
console.log("Hello World!!");
}
module.exports.func = func;
})
```
And it is called <mark>module wrapper function</mark>.
### Path module
```javascript=
var path = require('path');
var pathObj = path.parse(__filename);
/* output:
{ root: '/',
dir: '/mnt/d/code/node.js',
base: 'path.js',
ext: '.js',
name: 'path' }
*/
```
### OS module
```javascript=
const os = require('os');
var totalMemory = os.totalmem();
var freeMemory = os.freemem();
console.log(`Total Memory: ${totalMemory}`);
//Total Memory: 8461193216
console.log(`Free Memory: ${freeMemory}`);
//Free Memory: 1103503360
```
### fs module
```javascript=
const fs = require('fs');
var files = fs.readdirSync('./');
console.log(files);
//[ 'app.js', 'fs.js', 'log.js', 'os.js', 'path.js' ]
fs.readdir('*',function(err,files) {
if (err) console.log('Error',err);
else console.log(files);
});
//Error { [Error: ENOENT: no such file or directory, scandir '*'] errno: -2, code: 'ENOENT', syscall: 'scandir', path: '*' }
```
### event module
```javascript=
const EventEmitter = require('events');
const emitter = new EventEmitter();
//register a listener
emitter.on('messageLogged',function(args) {
console.log('the event is happened');
});
//raise an evetn. that means make a event happened
emitter.emit('messageLogged');
//if you want to send some messages.
emitter.emit('messageLogged',{message:'hello world'});
```
the above example, EventEmitter is a class. certainly, we can use extends.
```javascript=
const EventEmitter = require('events');
class myClass extends EventEmitter {
}
```
### HTTP module
```javascript=
const http = require('http');
const server = http.createServer();
server.o('connection', function(socket) {
console.log("New Connection...");
})
//make the sever listen onthe port 3000
server.listen(3000);
console.log("the server is listening on port 3000...");
```
and we can set the various routes in server.
```javascript=
const http = require('http');
const server = http.createServer(function(req,res) {
if (req.url == '/') {
res.write("Hello world!!");
res.end();
}
if (req.url == '/url') {
//statements
}
});
//make server listen on port 3000
server.listen(3000);
```