Node.js – Building First Application

  • by
node-js-express-app-first-result

In the previous post we saw steps for installation of Node.js on Windows machine. In this post we create our first Node.js app. We will first create a very simple Hello Node.js application and then move to creating a web app. Open your Visual Studio code and click on Open folder option. Select a folder where you wish to create your first application. My folder path: D:\nodejs\FirstApp

node-js-visual-studio-code-first-app-001

Click on “Open Folder” option and navigate to your project folder. After navigating to your Node.js project folder click on “Select Folder” option.

node-js-first-app

Your folder will be opened in Visual studio code. Open a new terminal window by clicking keys

Control + Shift+ ` together.

Your folder path will be shown like below. Type command npm init to create Node.js project inside this folder. You can provide the details like package name, version, description, entry point (index.js will be created by default if not entered anything). I have skipped everything by just pressing “Enter” and using default ones. Once completed, you will be able to see newly created package.json file inside of your folder.

skip-steps-create-project-node-js-init

Creating Index.js file:

Create a new js file with name “Index.js” by selecting new file option (shown with arrow in the image below). This file will be the entry point and main file of our app.

node.js first application index.js creation

Edit Index.js as below: Index.js

console.log('Hello Node.js');
console.log(100 * 7);

Now open the terminal windows by pressing Control + Shift+ ` keys together and type command node index to run the index.js file.

Output:

node.js first application output 002


Now we will create first server-side app using Node.js and Express:

What is Express in Node.js?

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

To install Express, type command npm install express in the terminal window

node-js-first-app-use-express

Now edit the index.js page as below. We will be using port no. 3000 for our app.

index.js:

const express = require('express');
const app = express();
const portNo = 3000;

app.get('/', (req, res) => {
res.send("Hi there. This is a simple Nodejs web page using Express");
});

app.listen(portNo, () => {
console.log('Server started! Visit localhost:' + portNo);
});

Save and on the terminal enter node indexcommand. This will run your app on port no. 3000. Open a browser window and navigate to URL http://localhost:3000/

node.js first application 001

In the next tutorial of this series we will more about NPM and Node.js modules.


 


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

2 thoughts on “Node.js – Building First Application”

  1. Pingback: Install and SetUp Node.js and NPM on Windows - ParallelCodes

  2. Pingback: Node.js Creating and Using Modules - ParallelCodes

Comments are closed.