Setting up Development Environment
Setting up a development environment is the first step in building and deploying applications. It involves installing the necessary tools and frameworks for development.
Install Node.js and npm
Node.js is a JavaScript runtime that allows us to run JavaScript code on the server-side. npm (Node Package Manager) is a package manager for JavaScript.
To install Node.js and npm, follow these steps:
Visit the official Node.js website (https://nodejs.org/) and download the LTS version for your operating system.
Follow the installation instructions to install Node.js and npm.
Verify the installation by running the following commands in your terminal:
1node --version
2npm --version
You should see the version numbers of Node.js and npm printed to the console, indicating that the installation was successful.
Setting up a Project
Once you have Node.js and npm installed, you can set up a new project by following these steps:
- Create a new directory for your project using the
mkdir
command. For example:
1mkdir my-project
- Navigate to the project directory using the
cd
command. For example:
1cd my-project
Initialize a new project using npm by running the
npm init
command. This will start a wizard that will guide you through the process of initializing a new project. You can press enter to accept the default options or customize them as needed.Install any dependencies that your project requires using the
npm install
command. For example, to install the Express.js framework, run the following command:
1npm install express
This will download and install the Express.js package into your project directory.
- Create a file to start your application. For example, create a file called
index.js
and add the following code:
1const express = require('express');
2const app = express();
3
4app.get('/', (req, res) => {
5 res.send('Hello, World!');
6});
7
8app.listen(3000, () => {
9 console.log('Server is running on port 3000');
10});
This code sets up a simple Express.js server that listens on port 3000 and sends a 'Hello, World!' message when you visit the root URL (http://localhost:3000/) in your browser.
- Start your application by running the following command in your terminal:
1node index.js
This will start the server, and you should see the message 'Server is running on port 3000' printed to the console.
// Install Node.js and npm
// Node.js is a JavaScript runtime that allows us to run JavaScript code on the server-side. npm (Node Package Manager) is a package manager for JavaScript.
// Visit the official Node.js website (https://nodejs.org/) and download the LTS version for your operating system.
// Follow the installation instructions to install Node.js and npm.
// Verify the installation by running the following commands:
xxxxxxxxxx
node --version
npm --version
// You should see the version numbers of Node.js and npm printed to the console.
// Setting up a Project
// Once you have Node.js and npm installed, you can set up a new project.
// 1. Create a new directory for your project:
xxxxxxxxxx
mkdir my-project
// 2. Navigate to the project directory:
xxxxxxxxxx
cd my-project
// 3. Initialize a new project using npm:
xxxxxxxxxx
npm init
// This command will start a wizard that will guide you through the process of initializing a new project. You can press enter to accept the default options or customize them as needed.
// 4. Install dependencies
// If you are using any third-party libraries or frameworks in your project, you can install them using npm. For example, to install the Express.js framework, run the following command:
xxxxxxxxxx
npm install express
// This will download and install the Express.js package into your project.
// 5. Create a file to start your application
// Create a new file called index.js
and add the following code:
xxxxxxxxxx
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
// This code sets up a simple Express.js server that listens on port 3000 and prints a 'Hello, World!' message when you visit the root URL.
// 6. Start your application
// Run the following command to start your application:
xxxxxxxxxx
node index.js
// You should see the message 'Server is running on port 3000' printed to the console.