- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Create a First App with Node.js
Creating your first Node.js application is simple and exciting. Follow this guide to build a basic HTTP server that responds with a greeting message.
Steps to Create Your First Node.js App
1. Set Up a Project Directory
- Create a directory for your app:
mkdir my-first-node-app
cd my-first-node-app
2. Initialize a Node.js Project
- Initialize a
package.json
file using npm:
npm init -y
- This creates a default
package.json
file for managing project dependencies.
3. Create the Application File
- Create a JavaScript file for your app:
touch app.js
4. Write the Code
- Open
app.js
in a code editor and add the following code:
const http = require('http');
// Create server
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, Node.js!');
});
// Listen on port 3000
server.listen(3000, () => {
console.log('Server is running at http://localhost:3000');
});
5. Run the Application
- Start the server:
node app.js
- Open your browser and navigate to
http://localhost:3000
.
6. Output
- Browser will display:
Hello, Node.js!
- Terminal will show:
Server is running at http://localhost:3000
Explanation of Code
1. http
Module
- Built-in module to create an HTTP server.
2. createServer
Method
- Handles incoming requests and sends responses.
3. Response Methods
res.writeHead
: Sets the HTTP status and headers.res.end
: Sends the response body and ends the request.
4. listen
Method
- Specifies the port number (3000 in this case) on which the server will listen.
Summary
In just a few steps, you’ve built your first Node.js application: a basic HTTP server. This foundation is a stepping stone to creating more complex applications, such as APIs, real-time services, and web apps. Happy coding!