Create Node.js application with Expressjs

Vibhu Ranjan
4 min readSep 17, 2020

--

If you are new to Node.js and want to start creating a simple project in Node.js with expressjs, you are landed on the right place.

  1. Install Node.js

First of all, you need to install Node.js from it’s official website. You can download it according to your platform. Click on the below link to download node.js:

I am using windows so it is showing me windows installers. Download the LTS version and install it with the default options.

Node.js

To check if Node.js is installed successfully, you can check it in command prompy(in window). If version is shown with the help of below command then Node.js is installed successfully in your machine.

2. Create project directory

We need to create a project folder where we can initialize our node js project. You can create a directory with the help of below command and navigate to that directory.

mkdir = This is used to make a directory.

cd = This is used to change a directory.

3. Initialize node.js project

Before initializing the project, we need to understand a little bit about npm. npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. All the packages we will use in our project will be installed from npm.

You can use the below command to initialize the project:

npm init

After typing yes, processing will be done and it will create package.json file inside the project directory. It contains all the information which we have provided at the time of initializing the project.

4. Create entry file index.js

As we have provided index.js as our entry or main file, we need to create index.js file inside project directory.

5. Install express

As we want to use express with node.js, we need to install express using npm command.

ExpressJS is a web application framework that provides you with a simple API to build websites, web apps and back ends. With ExpressJS, you need not worry about low level protocols, processes, etc.

Use the below command in mydemoapp directory only

npm install express — save

It will create node_modules folder and package-lock.json file inside mydemoapp project folder.

6. Add code to index.js

Add the following code in index.js file:

var express = require(‘express’);
var app = express();
app.get(‘/’, function (req, res)
{
res.send(‘Hey there, we are trying to learn node js with express.’);
});
app.get(‘/test’, function (req, res)
{
res.send(‘This is test url.’);
});
app.listen(3000, function () {
console.log(‘Example app listening on port 3000!’);
});

Here we are including express module in first line. We are assigning it into a variable which will provide access to predefined functions inside this module. In variable app, we are initializing the module to use the predefined functions in it.

app.listen() method starts a server and listens on port 3000 for connections. You can use any port here. It responds with “Hey there, we are trying to learn node js with express.” for get requests to the root URL (/). For /test URL, it will respond with “This is test url”. For every other path, it will respond with a Cannot GET.

7. Run the app

Use the below command to run the application

node index.js

It will print the message on the screen which we used in listen method.

After this, type the localhost with port name in the browser http://localhost:3000/. It will launch the application with default root URL(/).

That’s it for today. You have created a node js project with expressjs. You can explore the file structure created in this tutorial.

If you like this, please provide your feedback by clicking on like button. Thank you in advance. :)

--

--