Skip to content

Commit 7b3d797

Browse files
authored
Nodejs module systems (thombergs#151)
* add code examples for nodejs modules * add new cases for es modules * add README
1 parent 0018736 commit 7b3d797

File tree

23 files changed

+331
-0
lines changed

23 files changed

+331
-0
lines changed

nodejs/modules/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

nodejs/modules/.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

nodejs/modules/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Examples for [How to Use Modules in Your NodeJS Application?](nodejs-module-systems)
2+
3+
This repository contains the source code of the article's examples. They cover the basics on how to use the module
4+
systems CommonJS and ES Modules in NodeJS.

nodejs/modules/package-lock.json

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejs/modules/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "nodejs-modules",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"format": "prettier --write ."
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/thombergs/code-examples.git"
11+
},
12+
"author": "Robert Dey",
13+
"license": "MIT",
14+
"bugs": {
15+
"url": "https://github.com/thombergs/code-examples/issues"
16+
},
17+
"homepage": "https://reflectoring.io/",
18+
"dependencies": {
19+
"chalk": "^4.1.2"
20+
},
21+
"devDependencies": {
22+
"prettier": "2.5.1"
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const Logger = require("./logger");
2+
3+
Logger.info(`${logger.defaultMessage} printed in blue`);
4+
Logger.error("some error message printed in red");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const chalk = require("chalk");
2+
3+
class Logger {
4+
static defaultMessage = "Hello World";
5+
6+
static info(message) {
7+
console.log(chalk.blue(message));
8+
}
9+
10+
static error(message) {
11+
console.log(chalk.red(message));
12+
}
13+
}
14+
15+
module.exports = Logger;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const http = require("http");
2+
3+
const server = http.createServer(function (_req, res) {
4+
res.writeHead(200);
5+
res.end("Hello, World!");
6+
});
7+
server.listen(8080);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const chalk = require("chalk"); // don't forget to install
2+
3+
console.log(chalk.blue("Hello world printed in blue"));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const logger = require("./logger");
2+
3+
logger.logInfo(`${logger.defaultMessage} printed in blue`);
4+
logger.logError("some error message printed in red");

0 commit comments

Comments
 (0)