Basics of Node.js
Node.js is a runtime environment for executing JavaScript code based on Google’s JavaScript Engine, V8
Node.js is basically like going to your console inside of a web browser. It is good for building APIs, and is one of the few ways to create a backend using JavaScript.
One of the main benefits is that Node.js is asynchronous, meaning that when fetching from an API rather than waiting until they get the result before the next step, Node will proceed to next next step while waiting. Now lets jump into some basics!
Download node here: https://nodejs.org/en/
mkdir app-namecd app-nameatom . (or editor of your choice)touch app.js
Enter some random code:
function sayHello(name) {
console.log("Hello " + name);
endsayHello("NAME")
Now run the server
node app.js
Now you will see the result of the console.log on your terminal!
Create a new module
messenger.js
Fill it in.
message(message) {
console.log(message)
}module.exports = message;
Export info from a module by doing
module.exports.functionName = functionName;
Now get the module that was previously exported in the App.js file.
require(./messenger)
This gives you whatever was the exported object.
You can set it equal to something
var messenger = require(./messenger)
Now call the imported function
messenger.message("HELLO GOOD SIR");
Run
node app.js
Now the new function should be called.
If you wanted to export just the function instead of an object with multiple things attached, you can just do
module.exports = message
Now you can call the imported function with just
message("HELLO GOOD SIR")
When you do the requires, if it is a built in module node will just get that. If it is not a built in module or has . or / in front then it will look through your folder for the module.
Add the following to your file
const EventEmitter = require('events');
EventEmitter is in caps because it is a class, not a function.
This gives us access to the event class, which contains a bunch of functions.
An event is a signal that something has changed.
Create an instance of the EventEmitter class
const emitter = new EventEmitter();
We can now raise an event.
emitter.emit('messageLogged')
This does nothing without a listener, which is a function that does something once an event is raised.
emitter.addListener('messageLogged', function(){
console.log("LISTENER CALLED")
});emitter.on('messageLogged', function(){
console.log("LISTENER CALLED")
});
addListener and on are the same thing. They take two arguments, the event that it is listening for, and the function it is to run upon detecting that event.
Event arguments are arguments that are data that are sent when an event is raised. Typically sent as an object.
emitter.emit('messageLogged', {id: 1, name: "Bob"})
To have the listener make use of this information:
emitter.on('messageLogged', function(arg){
console.log(arg)
});
If you run it again it will print out the object from emit.
You can also use an arrow function:
emitter.on('messageLogged', (arg) => {
console.log(arg)
});
But the event that raises the notification should be in messenger, together with the message function, so move that over to messenger.js. Don’t forget to also require and make an instance of the EventEmitter class.
But if you run it now the event listener won’t notify you of the event. This is because both modules have a different instance of the EventEmitter class.
So rather than working with the EventEmitter directly, you create a class that acts similarly. We will use the Messenger class for this. Don’t forget to remove the word function before your functions now, as classes don’t need it. Also change the export to export the class.
const EventEmitter = require('events');
const emitter = new EventEmitter();class Messenger {
log(message) {
console.log(message)
emitter.emit('messageLogged', {id: 1, name: "Bob"})
}}module.exports = Messenger;
Adding “extends EventEmitter” to a class gives it all of the functionality of the EventEmitter class. You then no longer need to make an instance for Logger to work with as well.
const EventEmitter = require('events');class Messenger extends EventEmitter{
log(message) {
console.log(message)
this.emit('messageLogged', {id: 1, name: "Bob"})
}}module.exports = Messenger;
Now back in app.js you no longer need the EventEmitter instance as well, since you are working with the one from Messenger. You instead use an instance of that.
const Messenger = require('./messenger');
const EventEmitter = require('events');
const messenger = new Messenger();function sayHello(name) {
console.log("Hello " + name);
}sayHello("NAME");messenger.on('messageLogged', function(arg){
console.log(arg)
});messenger.message("HELLO GOOD SIR");
So we can now create a server, and we have our code set up nicely by breaking it down into several modules. Now let’s move on to one of its biggest draws: being used as an API.
The HTTP Module is used for creating backend apis.
const http = require('http');
const server = http.createServer();
This server is also an EventEmitter. Every time there is a new connection or request the server raises an event.
server.on('connection', (socket) => {
console.log("New Connection");
})server.listen(3000);
console.log('listening on port 3000')
Now if you go to
http://localhost:3000/
Your terminal will print out “New Connection!”
We can change it so that instead of console.logging, upon receiving a connection there is a response based on the connection.
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.write('Hello World');
res.end();
} else if (req.url === '/api/heroes') {
res.write(JSON.stringify(["Superman", "Batman", "Spiderman"]));
res.end()
}
});
If you stop and restart your server on your terminal, you will see that now the page says “Hello World”. It displays this because we connected with just localhost3000. But if you instead connect with localhost3000/api/heroes now you will instead see an array of objects that are in JSON form!
Instead of listening only on port 3000, set it to find the assigned port and listen there.
const port = process.env.PORT || 3000app.listen(port, () => {
console.log(`server on port ${port}`);
});
We have learned how to use Node.js to run code, and how to use it to set up a site that functions like a basic API. Stay tuned for my upcoming article on using Node.js, Express, and MongoDB to create an API with CRUD (create, read, update, delete) functionality!