Welcome to the tutorial on web development using Node.js! Node.js is a powerful JavaScript runtime that allows you to build scalable network applications using JavaScript. This guide will take you through the basics of Node.js, from installation to building a simple web server.
Getting Started
Before you begin, make sure you have Node.js and npm (Node Package Manager) installed on your system. You can download Node.js from the official website.
Install Node.js
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
Create a Project Directory
Create a new directory for your project and navigate into it:
mkdir my-nodejs-project
cd my-nodejs-project
Initialize Your Project
Initialize a new Node.js project by running:
npm init -y
This command creates a package.json
file that contains metadata about your project and its dependencies.
Install Express.js
Express.js is a popular web application framework for Node.js. Install it by running:
npm install express
Create a Simple Web Server
Create a file named server.js
in your project directory and add the following code:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Run Your Server
Run your server by executing the following command in your terminal:
node server.js
Your server should now be running on http://localhost:3000
. Open your web browser and navigate to that URL to see the "Hello, World!" message.
Next Steps
Now that you have a basic understanding of how to create a web server with Node.js, you can explore more advanced topics such as routing, middleware, and database integration. Check out our Node.js Advanced Topics tutorial for more information.