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 Di…


This content originally appeared on DEV Community and was authored by Christopher Glikpo

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

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 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 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.
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');

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 want to learn more about Web Development don't forget to to follow me on Youtube!


This content originally appeared on DEV Community and was authored by Christopher Glikpo


Print Share Comment Cite Upload Translate Updates
APA

Christopher Glikpo | Sciencx (2021-06-26T14:58:45+00:00) Getting Started with Express. Retrieved from https://www.scien.cx/2021/06/26/getting-started-with-express/

MLA
" » Getting Started with Express." Christopher Glikpo | Sciencx - Saturday June 26, 2021, https://www.scien.cx/2021/06/26/getting-started-with-express/
HARVARD
Christopher Glikpo | Sciencx Saturday June 26, 2021 » Getting Started with Express., viewed ,<https://www.scien.cx/2021/06/26/getting-started-with-express/>
VANCOUVER
Christopher Glikpo | Sciencx - » Getting Started with Express. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/26/getting-started-with-express/
CHICAGO
" » Getting Started with Express." Christopher Glikpo | Sciencx - Accessed . https://www.scien.cx/2021/06/26/getting-started-with-express/
IEEE
" » Getting Started with Express." Christopher Glikpo | Sciencx [Online]. Available: https://www.scien.cx/2021/06/26/getting-started-with-express/. [Accessed: ]
rf:citation
» Getting Started with Express | Christopher Glikpo | Sciencx | https://www.scien.cx/2021/06/26/getting-started-with-express/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.