Getting Started with Node.js
- meFqulted
- January 13, 2024
Getting Started with Node.js
Welcome to the world of Node.js! Whether you’re a beginner or an experienced developer, Node.js is a powerful tool that allows you to build scalable and efficient server-side applications. In this lesson, we’ll cover the basics of Node.js and how to set up a simple server.
What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It allows you to build server-side applications using JavaScript, which was traditionally used only for client-side scripting in web browsers.
Installing Node.js
Before we dive into coding, you need to install Node.js on your machine. Visit the official Node.js website and download the latest version for your operating system. Follow the installation instructions to complete the setup.
To check if Node.js is installed, open your terminal or command prompt and type:
node -v
If installed correctly, this command will display the installed Node.js version.
Your First Node.js Application
Now that you have Node.js installed, let’s create a simple “Hello world” application.
- Create a new file, let’s call it
app.js
, using your favorite text editor. - Add the following code:
console.log('Hello world!');
-
Save the file.
-
Open your terminal or command prompt, navigate to the directory where
app.js
is located, and run:
node app.js
You should see the message “Hello world!” printed in the terminal.
Creating a simple webserver using Express
Let’s use Express! Express is a popular web framework for Node.js that simplifies the process of building robust and scalable web applications.
Installing Express
Before using Express, you need to install it. Open your terminal or command prompt, navigate to your project directory, and run:
npm init -y
npm install express
This initializes a new package.json
file and installs the Express module.
We are going to comment on certain areas of the code to explain what it does.
Your First Express Application
Now, let’s modify the app.js
file to use Express:
const express = require('express');
const app = express();
// Define a route that responds with "Hello, Express!"
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
// Start the server
app.listen(3000, () => {
console.log(`Server running at http://localhost:3000/`);
});
Save the file and run the application:
node app.js
Visit http://localhost:3000
in your web browser, and you should see “Hello, Express!“.
Adding Middleware
Express allows you to use middleware to perform tasks during the request-response cycle. Let’s add a middleware that logs information about each incoming request:
const express = require('express');
const app = express();
// Middleware to log incoming requests
app.use((req, res, next) => {
console.log(`Received a ${req.method} request to ${req.url}`);
next();
});
// Define a route that responds with "Hello, Express!"
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
// Start the server
app.listen(3000, () => {
console.log(`Server running at http://localhost:3000/`);
});
Now, when you make a request, you should see information about the request logged in the terminal.
Congratulations! You’ve just created a basic Node.js application and a simple webserver. This is just the beginning, and there’s a lot more to explore and learn in the vast Node.js ecosystem. Happy coding!