Getting Started with Express
Tags: javascript, webdev, express, tutorial
Published: 2021-06-26 14:58:45

In the [previous article](https://dev.to/cglikpo/getting-started-with-nodejs-130h), 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](https://dev.to/cglikpo/getting-started-with-nodejs-130h) 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
```
Step 2: navigate into the folder or the directory created in command prompt using the commands:
```
cd learn-express
```

## 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
```

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"
}
```
## What is express
[Express](https://expressjs.com/) 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
```

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"
  }
}
```

## 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}!`)
});
```
## Explanation of the above code:
### Require
[Require](https://nodejs.org/api/modules.html#modules_require_id) are used  to import `modules`, `JSON`, and `local files`. `Modules` can be imported from `node_modules`.

Example:
```
const yourModule = require( "your_module_name" );
```
ExpressJS  is a [NodeJs Module](https://nodejs.org/api/modules.html#modules_modules).
`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](https://nodejs.org/api/all.html#all_require), 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');
```
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.
```

You can start the server by calling node with the script in your command prompt:
```
>node main
App listening on port 8000
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm11knj7d8zwcvo150q2.png)](https://www.buymeacoffee.com/cglikpo)
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!](https://www.youtube.com/c/ChristopherGlikpo/featured)