Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Node.js Tutorial

Introduction to Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It is built on Chrome's V8 JavaScript engine and allows developers to use JavaScript to write server-side code.

Installing Node.js

To install Node.js, follow the steps below:

1. Go to the official Node.js website at https://nodejs.org.

2. Download the installer for your operating system.

3. Run the installer and follow the instructions.

4. Verify the installation by opening a terminal or command prompt and typing:

node -v

This should display the installed Node.js version.

Creating a Simple Node.js Application

Let's create a simple Node.js application that prints "Hello, World!" to the console.

1. Create a new directory for your application:

mkdir my-node-app

2. Navigate to the new directory:

cd my-node-app

3. Create a new file named app.js and open it in your favorite text editor.

4. Add the following code to app.js:

console.log('Hello, World!');

5. Save the file and return to your terminal or command prompt.

6. Run the application:

node app.js

You should see the following output:

Hello, World!

Working with HTTP in Node.js

Node.js provides an HTTP module that allows you to create a web server. Let's create a simple web server that responds with "Hello, World!" to every request.

1. Create a new file named server.js and open it in your text editor.

2. Add the following code to server.js:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
                    

3. Save the file and return to your terminal or command prompt.

4. Run the server:

node server.js

You should see the following output:

Server running at http://127.0.0.1:3000/

5. Open a web browser and navigate to http://127.0.0.1:3000/. You should see "Hello, World!" displayed on the page.

Integrating Kafka with Node.js

Apache Kafka is a distributed streaming platform used for building real-time data pipelines and streaming applications. To integrate Kafka with Node.js, we can use the kafka-node package.

1. First, install the kafka-node package:

npm install kafka-node

2. Create a new file named kafka-producer.js and open it in your text editor.

3. Add the following code to kafka-producer.js to create a Kafka producer:

const kafka = require('kafka-node');
const Producer = kafka.Producer;
const client = new kafka.KafkaClient({ kafkaHost: 'localhost:9092' });
const producer = new Producer(client);

const payloads = [
  { topic: 'test', messages: 'Hello Kafka', partition: 0 }
];

producer.on('ready', () => {
  producer.send(payloads, (err, data) => {
    console.log(data);
  });
});

producer.on('error', (err) => {
  console.error('Error:', err);
});
                    

4. Save the file and run the producer:

node kafka-producer.js

The producer will send a message to the Kafka topic named "test".

5. Next, create a new file named kafka-consumer.js and open it in your text editor.

6. Add the following code to kafka-consumer.js to create a Kafka consumer:

const kafka = require('kafka-node');
const Consumer = kafka.Consumer;
const client = new kafka.KafkaClient({ kafkaHost: 'localhost:9092' });
const consumer = new Consumer(client, [{ topic: 'test', partition: 0 }], { autoCommit: true });

consumer.on('message', (message) => {
  console.log(message);
});

consumer.on('error', (err) => {
  console.error('Error:', err);
});
                    

7. Save the file and run the consumer:

node kafka-consumer.js

The consumer will receive and log messages from the Kafka topic named "test".

Conclusion

In this tutorial, we covered the basics of Node.js, including installation, creating a simple application, working with HTTP, and integrating Kafka. Node.js is a powerful tool for building server-side applications, and its integration with Kafka allows for robust real-time data processing.