Create your first Node.js app with Express

Create Your First Node.js App With Express

In this article, you’ll learn how to create your first Node.js app with Express. We’ll go over some basic configuration like setting up your project with NPM, installing dependencies, and finally how to manage a server with Express.

Initializing the Project With NPM

The steps to take to create the project are quite simple. Let’s see how to initialize the project with NPM.

First, create a folder with any name you like. Once created, open the terminal and navigate to it.

Now type the following command :

npm init

After typing the command you will be asked for various information, such as the name of the app, the author and so on, for the moment leave the default configuration and press enter. 

Create your first Node.js app with Express

At this point NPM will have created a file called package.json in the folder of your choice. This file contains metadata relevant to the project and is used to manage dependencies, scripts, project version and much more.

//package.json
{
  "name": "node-starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Lorenzo Miscoli",
  "license": "ISC"
}

Express Installation

Now it’s time to install the Express dependency.

Still from the terminal, type the following command :

npm i express

This command has the function of querying the npm repository, downloading the dependency, and inserting it into the dependency list in our package.json.

Every dependency that we are going to install will be downloaded in the node-modules folder of our project.

Creating the Main File

The main file is the file that starts the application. By default in the package.json it is called index.js. Create the above file in the root of your project, and type the following code.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`App listening on port:${port}`)
})

In this file we define a server listening on port 3000, after which we register the path /.

Running the App

Now that the project is ready, it’s time to test if everything works. Type the following command from the terminal:

npm index.js

With this command you run the index.js file, which in turn starts the server. You can also register a script by entering “start”: “node index.js” in the scripts list of your package.json and then run the npm start command.

Running Node.js app

Open your browser, and type the following url : http://localhost:3000

Hello World with Node.js and Express

Conclusion

In this article we have seen how it is possible to create a Node.js app with Express. We’ve also seen how to initialize a project and manage dependencies with NPM. Of course, there’s so much more to learn about Node.js and Express that can’t be wrapped up in a single article.
Source code is on GitHub.

Lorenzo Miscoli

Software Developer specialized in creating and designing web applications. I have always loved technology and dreamed of working in the IT world, to make full use of my creativity and realize my ideas.
Scroll to Top