The Basic of Node.JS (before Classes)

Must Watch Videos:

Quick Assessments

PART ONE: Read a local JOSN file (data.json); find the user record of firstname = ‘jane’; capitalize the the first letter to ‘Jane’ ( not ALL records, just jane ); update the JSON object

data.json example from https://www.learningcontainer.com/sample-json-file/#JSON_file_example_1{
“users”: [
{
“userId”: 1,
“firstName”: “Krish”,
“lastName”: “Lee”,
“phoneNumber”: “123456”,
“emailAddress”: “krish.lee@learningcontainer.com”
},
{
“userId”: 2,
“firstName”: “racks”,
“lastName”: “jacson”,
“phoneNumber”: “123456”,
“emailAddress”: “racks.jacson@learningcontainer.com”
},
{
“userId”: 3,
“firstName”: “denial”,
“lastName”: “roast”,
“phoneNumber”: “33333333”,
“emailAddress”: “denial.roast@learningcontainer.com”
},
{
“userId”: 4,
“firstName”: “devid”,
“lastName”: “neo”,
“phoneNumber”: “222222222”,
“emailAddress”: “devid.neo@learningcontainer.com”
},
{
“userId”: 5,
“firstName”: “jone”,
“lastName”: “mac”,
“phoneNumber”: “111111111”,
“emailAddress”: “jone.mac@learningcontainer.com”
}
]
}

Sample Solution (using ‘require’)

const database = require(‘./data.json’);// Replace firstName jane with first lette capitalized
for(var key in database.users){
if ( fn = database.users[key].firstName.match(/jone/) ){
database.users[key].firstName = fn.input[0].toUpperCase() + fn.input.slice(1);
}
}
console.log(database);

Output

jane is updated to Jane

PART TWO: Continue with Part One, but loading a remote JSON file ( https://jsontestfiles2022.s3.us-east-1.amazonaws.com/data.json )

Example Solution

const https = require('https');

let url = "https://jsontestfiles2022.s3.us-east-1.amazonaws.com/data.json";

https.get(url,(res) => {
let body = "";

res.on("data", (chunk) => {
body += chunk;
});

res.on("end", () => {
try {
database = JSON.parse(body);
for(var key in database.users){
if ( fn = database.users[key].firstName.match(/jone/) ){
database.users[key].firstName = fn.input[0].toUpperCase() + fn.input.slice(1);
}
}
console.log(database);
} catch (error) {
console.error(error.message);
};
});

}).on("error", (error) => {
console.error(error.message);
});

PART THREE: Create and Load Module — app.js ( this main ) loading logger.js ( module ) to echo out a message

logger.js

function logger ( message ) {
console.log(message);
}

module.exports = logger;

app.js

var log = require ('./logger3');

log('Part 3');

Output:

Part 3

PART FOUR: Events

1. register a Listener (messageLogged) to console log a JSON message
2. raise an Event (messageLogged) and send over an JSON of two elements id and name { id: 100, name: “Max }

const EventEmitter = require ('events');
const emitter = new EventEmitter();

// Register a Listener to console.log of the JSON object
emitter.on('messageLogged', ( noise ) => {
console.log("Message Logged: " + JSON.stringify(noise) );
})

// Raise an Event and passing id and name
emitter.emit('messageLogged', {id: 100, name: "Max"});

###

--

--

--

“Our industry does not respect tradition — it only respects innovation.”

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Javascript Basic Concept 1 to 10

JavaScript Engine

How to get connected to a Database and work with it via ADO and FireDAC

Resources for Launching Your JavaScript Career

Interview preparations for Angular developer

Leetcode (26): Remove Duplicates from Sorted Array

What is a Component Library?

Literals ! Template Literals!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
@maxtsai

@maxtsai

“Our industry does not respect tradition — it only respects innovation.”

More from Medium

Database connection pool size

CS371p Spring 2022: Vincent Huynh

Diving into design as a Front End Developer

CS371p Spring 2022 Blog Wk14: Cristian Astorga