Modern Node.js Explained For Beginners

- Published on

Node.js is a powerful open-source server-side runtime environment that allows developers to build fast, scalable, and efficient web applications. Node.js is based on the JavaScript programming language, and it's known for its ability to handle a large number of concurrent connections and real-time applications.
If you're a beginner and want to get started with Node.js, this blog post will guide you through the basics of Node.js and help you get up to speed with modern Node.js development.
Installation
The first step in working with Node.js is to install it on your machine. You can download the Node.js installer from the official website and follow the installation instructions.
Once you have Node.js installed on your machine, you can start creating Node.js applications using your favorite text editor or integrated development environment (IDE).
Creating a Simple Node.js Application
Let's start by creating a simple Node.js application that prints "Hello, World!" to the console. To do this, create a new file called "app.js" and add the following code:
console.log('Hello, World!')
Save the file and open a terminal window. Navigate to the directory where the "app.js" file is located and type the following command:
node app.js
You should see the following output in the terminal:
Hello, World!
Congratulations! 🎉 You have created your first Node.js application.
Working with Modules
Node.js has a built-in module system that allows you to organize your code into reusable modules. You can create a module by creating a new file and exporting its contents using the module.exports object.
Let's create a module that calculates the sum of two numbers. Create a new file called "calculator.js" and add the following code:
function add(a, b) {
return a + b
}
module.exports = {
add,
}
In this code, we have defined a function called add that takes two arguments and returns their sum. We have exported this function using the module.exports object, which makes it available for other modules to use.
Now, let's use this module in our "app.js" file. Add the following code to the "app.js" file:
const calculator = require('./calculator')
console.log(calculator.add(2, 3))
In this code, we are requiring the "calculator" module using the require function and assigning it to a variable called calculator. We are then calling the add function of the calculator module with two arguments and printing the result to the console.
Running this code should output the following:
5
Node.js is commonly used to build web applications, and it comes with a built-in HTTP module that allows you to create an HTTP server and handle incoming requests.
Let's create a simple HTTP server that responds with "Hello, World!" to any incoming requests. Add the following code to a new file called "server.js":
const http = require('http')
const server = http.createServer((req, res) => {
res.write('Hello, World!')
res.end()
})
server.listen(3000, () => {
console.log('Server is listening on port 3000')
})
In this code, we are creating an HTTP server using the http.createServer function. We are then defining a callback function that is called whenever an incoming request is received. In this function, we are writing "Hello, World!" to the response and then ending the response using the res.end() function.
Finally, we are starting the server and listening on port 3000 using the server.listen function. When the server starts listening the callback function passed to server.listen is executed, and we print a message to the console.
To test the server, open a web browser and navigate to http://localhost:3000. You should see the message "Hello, World!" displayed in your browser.
Asynchronous Programming
Node.js is designed to handle asynchronous programming using callback functions, promises, and async/await syntax. Asynchronous programming is essential in Node.js to avoid blocking the event loop and allow multiple requests to be handled concurrently.
Let's create an asynchronous function that simulates a delay of 2 seconds before returning a value. Add the following code to a new file called "async.js":
function delay() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello, World!')
}, 2000)
})
}
async function run() {
const result = await delay()
console.log(result)
}
run()
In this code, we are defining a function called delay that returns a new promise that resolves after 2 seconds with the value "Hello, World!". We are then defining an async function called run that waits for the delay function to complete using the await keyword and then prints the result to the console.
When we run this code using node async.js, we should see the message "Hello, World!" printed to the console after a 2-second delay.
Conclusion
Node.js is a powerful and versatile platform for building web applications, command-line tools, and other server-side applications. In this blog post, we have covered some of the basics of modern Node.js development, including installing Node.js, creating a simple application, working with modules, handling HTTP requests, and asynchronous programming.
By mastering these fundamental concepts, you'll be well on your way to building more complex and sophisticated Node.js applications. Happy coding!
Did you enjoy reading?
Follow Me !
If you enjoyed this article, follow me on social media for more thoughts on full-stack development particularly in the web3 space!