DEV Community

Cover image for Getting Started with Express
Christopher Glikpo
Christopher Glikpo

Posted on • Edited on

Getting Started with Express

In the previous article, I explained getting started with NodeJS.In this article,I am assuming you already have node installed on your pc,if not I recommend you read my previous article on getting started with NodeJS.

This article covers:

  • Create Directory and Navigate into directory
  • Create package.json file
  • What is express
  • Install express
  • Create a new file
  • Basic Example

Create Directory and Navigate into directory

Step 1: create a directory or folder for your new application anywhere on your desktop in Command Prompt:

mkdir learn-express
Enter fullscreen mode Exit fullscreen mode

Step 2: navigate into the folder or the directory created in command prompt using the commands:

cd learn-express
Enter fullscreen mode Exit fullscreen mode

Create package.json file

Use the npm init -y command to create a package.json file for your application in the same directory.

npm init -y
Enter fullscreen mode Exit fullscreen mode

If you display the package.json file , you will see the defaults that you accepted, ending with the license.

{
  "name": "learn-express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

What is express

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

Install express

Express is not a native package to Node, so it must be installed. Because you want to ensure it's included in your node modules, make sure to install it locally and then require it in your server.

Now install Express in the learn-express directory by running the command npm install express in command prompt:

npm install express
Enter fullscreen mode Exit fullscreen mode

The dependencies section of your package.json will now appear at the end of the package.json file and will include Express.

{
  "name": "learn-express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create a new file

Create a new file inside learn-express directory,you can call it any name but I am going to call it main.js.

Basic Example:

Inside the file created add the following code:

const express = require('express');
const app = express();
const port =process.env.port || 8000;

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

app.listen(port, () => {
  console.log(`App listening on port ${port}!`)
});
Enter fullscreen mode Exit fullscreen mode

Explanation of the above code:

Require

Require are used to import modules, JSON, and local files. Modules can be imported from node_modules.

Example:

const yourModule = require( "your_module_name" );
Enter fullscreen mode Exit fullscreen mode

ExpressJS is a NodeJs Module.
express is the module's name, as well as the name we usually assign to the variable we use to refer to the module's main function in code like the one you mentioned.

NodeJS provides the require function, whose job is to load modules and give you access to their exports.

You don't have to call the variable express, you can do

var myvariable = require('express');
Enter fullscreen mode Exit fullscreen mode

and use myvariable instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.

Express's default export is a little unique in that it's a function with properties that are themselves functions (methods). This is absolutely acceptable in JavaScript, but not so much in other languages. That's why, in addition to using express() to build an Application object, you can also use express.static(/*...*/) to set up serving static files.

JavaScript Note:

The backticks in the `App listening on port ${port}!` let us interpolate the value of $port into the string.
Enter fullscreen mode Exit fullscreen mode

You can start the server by calling node with the script in your command prompt:

>node main
App listening on port 8000
Enter fullscreen mode Exit fullscreen mode

If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.

If you like my work, please consider
Buy me a coffee
so that I can bring more projects, more articles for you

If you want to learn more about Web Development don't forget to to follow me on Youtube!

Top comments (0)