The Wayback Machine - https://web.archive.org/web/20240622223139/https://www.geeksforgeeks.org/how-to-run-node-js-server/
Open In App

How to Run Node Server ?

Last Updated : 07 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Node is just a way for you to run JavaScript outside the browser. It can be used to run desktop app servers or anything else that you want to do with JavaScript and the thing that we are going to do is create a web server using NodeJS.

Approach to Run Node Server

  • We have created a server variable here that uses the HTTP library and called the create server function on this object and this creates a server function that has two parameters which are the request and response parameters.
  • Set up our server so it will listen on the port that we want so that we have this server object pass it that port variable that we created to tell it to listen on port 8080 and then this takes a single function that it will call if there is an error potentially or it was successful.

Steps to Create Project and Module Installation

Step 1: You can visit the link Download Node and download the LTS version. After installing the node you can check your node version in the command prompt using the command.

node --version

Step 2: Create a new folder for a project using the following command:

mkdir testApp

Step 3: Navigate to our folder using the following command:

cd testApp

Step 4: Initialize npm using the following command and server file:

npm init -y

Step 5: Creating an app.js file with the following code. Inside this file we need to create our server and tell to start listening on a certain port, So firstly we need to require a certain library called HTTP which will preclude the HTTP library into our code inside of this HTTP variable that we created.

Screenshot-2024-06-06-161907

Project Structure:

NodeProj

Project Structure

Example: Below is the code for the basic server of the nodejs.

Node
//app.js

const http = require('http')
const port = 8080

// Create a server object:
const server = http.createServer(function (req, res) {

    // Write a response to the client
    res.write('Hello World')

    // End the response 
    res.end()
})

// Set up our server so it will listen on the port
server.listen(port, function (error) {

    // Checking any error occur while listening on port
    if (error) {
        console.log('Something went wrong', error);
    }
    // Else sent message of listening
    else {
        console.log('Server is listening on port' + port);
    }
})

Steps to run: Run the application using the following command.

node app.js

Output: Now open your browser and go to http://localhost:8080/, you will see the following output.

Output


Similar Reads

Create a web server and run PHP script on it using Raspberry Pi
In this post, creating a web server and running PHP script on it using Raspberry Pi will be discussed. Web Servers are basically simple computer programs that dispense the web page when they are requested using the web client. The machines on which this program run are usually called as a server, with both the names web server and server almost use
2 min read
How to run two PHP versions for two projects on same apache server ?
In this article, we will learn about running two versions of PHP on the same Apache server. To manage many domains, the Apache webserver uses virtual hosts on a single instance. Prerequisite: We will be using the XAMPP server to use the Apache server and some knowledge to use it. First, download the older version of XAMPP that can be of any version
2 min read
How to run ExpressJS server from browser ?
Express is the node module which is used to create a server, to take get request on our server. We can run the ExpressJS server from the browser using the following 5 simple steps: Step 1: Install the Express locally into your system by the following command: npm install express Step 2: Check the version of the express server by using the below com
2 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Run Python script from Node.js using child process spawn() method
Node.js is one of the most adopted web development technologies but it lacks support for machine learning, deep learning and artificial intelligence libraries. Luckily, Python supports all these and many more other features. Django Framework for Python can utilize this functionality of Python and can provide support for building new age web applica
3 min read
How to run Cron Jobs in Node.js ?
Cron Jobs: These are the tasks that run periodically by the operating system. Users can schedule commands the OS will run these commands automatically according to the given time. It is usually used for system admin jobs such as backups, logging, sending newsletters, subscription emails and more. Prerequisites: Node.js installed NPM installed Basic
3 min read
How to Run Synchronous Queries using sync-sql Module in Node.js ?
The sync-sql module is designed to make synchronous queries to the database. Since normal SQL queries are asynchronous in node.js but if you want to run it synchronously, then it is possible using this module. In synchronized SQL, the result set is returned when the query is executed. Note: Do not use this module in production mode as node.js is de
2 min read
Run Python Script using PythonShell from Node.js
Nowadays Node.js is the most attractive technology in the field of backend development for developers around the globe. And if someone wishes to use something like Web Scraping using python modules or run some python scripts having some machine learning algorithms, then one need to know how to integrate these two. We are going to get some data of a
3 min read
How to Run Multiple Versions of Node.js ?
Usually, we work on different versions for our Node.js project and it's hard to manage them, but fortunately, there is a tool called NVM(node version manager) which helps to manage your node version and switch between them according to your projects. Install NVM Module: You can install the nvm module using the following command: Using curl: $ curl
2 min read
How to run a node.js application permanently ?
NodeJS is a runtime environment on a V8 engine for executing JavaScript code with some additional functionality that allows the development of fast and scalable web applications but we can't run the Node.js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or
2 min read
three90RightbarBannerImg