- 4 months ago
- Zaid Bin Khalid
- 424 Views
-
2
Node.js is a powerful runtime environment that allows you to run JavaScript on the server-side. It’s built on the V8 JavaScript runtime engine, which powers Google Chrome. Node.js enables developers to write server-side applications using JavaScript, offering a non-blocking, event-driven architecture that makes it particularly efficient for building scalable and real-time applications.
To get started with Node.js, follow these steps:
- Installation:
- Visit the official Node.js website (https://nodejs.org/) and download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the installation instructions. Node.js includes npm (Node Package Manager) by default, which you’ll use to manage packages and dependencies for your Node.js projects.
- Verification:
- After installation, open your terminal or command prompt.
- Type
node -v
and press Enter. This command will display the installed version of Node.js.
- Similarly, type
npm -v
to check the version of npm.
- Creating a Node.js Project:
- Choose or create a directory for your project.
- Open your terminal or command prompt and navigate to the project directory.
- Run
npm init
to initialize a new Node.js project. Follow the prompts to create a package.json
file, which will store metadata about your project and its dependencies.
- Writing Your First Node.js Script:
- Create a new JavaScript file within your project directory, for example,
app.js
. Open the file in a text editor and write your JavaScript code. For example:
- Running Your Node.js Script:
- In your terminal or command prompt, navigate to your project directory.
- Type
node app.js
and press Enter. This will execute your Node.js script, and you should see the output Hello, Node.js!
.
App JS
// app.js
console.log("Hello, Node.js!");
Node JS server
To create a basic server in Node.js, you can use the built-in http
module, which provides functionality for creating HTTP servers. Here’s a step-by-step guide on how to create a simple HTTP server and start it in Node.js:
- Create a new directory for your project (e.g.,
node-server
).
- Navigate into the
node-server
directory in your terminal or command prompt.
- Create a new file named
server.js
.
- Open
server.js
in a text editor and add the following code:
// server.js
// Import the http module
const http = require('http');
// Define the hostname and port number
const hostname = '127.0.0.1';
const port = 3000;
// Create the HTTP server
const server = http.createServer((req, res) => {
// Set the HTTP status code and content type
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
// Write the response body
res.end('Hello, Node.js HTTP Server!');
});
// Start the server and listen for incoming requests
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
- Save the file.
- In your terminal or command prompt, navigate to the
node-server
directory.
- Run the following command to start the server:
You should see the following output:
Server running at http://127.0.0.1:3000/
Your Node.js HTTP server is now running and listening for incoming requests. You can access it by visiting http://127.0.0.1:3000/
in your web browser or by making HTTP requests to that address programmatically. When you access the server in your browser, you should see the message “Hello, Node.js HTTP Server!” displayed on the page.
- 4 months ago
- Zaid Bin Khalid
- 424 Views
-
2