Getting Started with Node.js and npm
Learn the basics of Node.js and npm, and create your first project.
Introduction to Node.js
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine that allows developers to create scalable and high-performance server-side applications. To get started with Node.js, you need to have it installed on your computer.
Step 1: Install Node.js and npm
You can download the latest version of Node.js from the official website. Once installed, open your terminal and type node -v to verify that Node.js is installed correctly.
node -v
Step 2: Create a New Project
Create a new directory for your project and navigate to it in your terminal. Then, run npm init to create a package.json file.
mkdir myproject
cd myproject
npm init
Step 3: Install Dependencies
Install the required dependencies for your project. For example, to install the express framework, run npm install express.
npm install express
Step 4: Create a Server
Create a new file called server.js and add the following code to create a simple server.
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
Step 5: Run the Server
Run the server by typing node server.js in your terminal.
node server.js
Open your web browser and navigate to http://localhost:3000 to see the 'Hello World!' message.