Writing Your First Node JS Application

Search for a command to run...

No comments yet. Be the first to comment.
In this tutorial series, we will understand the Fundamentals of NodeJs and learn to use Javascript on the server side.
In this section, let's try to understand the difference between the Nodejs and Browser JS Both the browser and Node.js use JavaScript as their programming language. Building apps that run in the browser is a completely different thing than building ...
You've done it. I've done it. We've all done it.

Your AI Agent Has Amnesia. ByteRover Fixes It — Without Vectors Let's see how to built a customer support agent that remembers everything — using plain markdown files, BM25 search, zero embeddings, a

My GenAI cohort kicked off today, and what a first day! Right away we were reminded that AI isn’t magic — at its core, models like Generative Pretrained Transformers (GPT) are doing one simple thing:

Hello Folks👋, I Recently got Graduated🎓 in 2020, yep in the Pandemic. We all hit very hard by this Corona Pandemic and we all got stuck at home for very long. We all realize the Importance of Socializing, Fitness (Physically as well Mentally), Diet...

So after learning the basic commands of git in our previous section, let's see Git in action. If you already have a project to track your files then you can skip this section and if not then continue. Initializing a Repository from Non-existing Dire...

The most common example Hello World of Node.js is a web server:
Before creating an actual "Hello, World!" application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components -
Create an app.js file and add the below code in it:
Step 1 - Import Required Module
We use the require directive to load the httpmodule and store the returned HTTP instance into an http variable as follows -
const http = require("http");
Step 2 - Define Port
We define our port application port on 3000. It basically means our app will be available on port 3000 and if we visit http://localhost:3000 we can access our app.
const port = 3000;
Step 3 - Create Server
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World!\n");
});
The createServer() method of http creates a new HTTP server and returns it. The server is set to listen on the specified port. When the server is ready, the callback function is called, in this case informing us that the server is running.
Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse object).
Those 2 objects are essential to handle the `HTTP call.
The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.
The second is used to return data to the caller.
In this case with:
res.statusCode = 200
we set the statusCode property to 200, to indicate a successful response.
Now, we set the Content-Type header:
res.setHeader('Content-Type', 'text/plain')
and we close the response, adding the content as an argument to end():
res.end('Hello World\n')
Step 4: Listen
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}/`);
});
listen()helps the server to listens for incoming traffic at port 3000 i.e. it waits for a request over 3000 port on the local machine and if any request arrives the server will handle/respond to that request.
Whole code in app.js will look like this:
// app.js
// import http module
const http = require("http");
// define port
const port = 3000;
// create server
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World!\n");
});
// listen on defined PORT
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}/`);
});
Now execute the app.js to start the server as follows -
node app.js
Open http://localhost:3000/ in any browser and observe the following result.
Live Code Example: