How to: Create an API Rest with Express - Basic
Tags: beginners, node, tutorial, javascript
Published: 2024-02-17 02:16:10

Hi everyone, I want to bring you a quick tutorial/example of make an API Rest with Express and create a Dockerfile for building a container image.

First, we need to initialize an NodeJS project

```Bash
npm init
```
In this step, we will ask for the next parameters for creating the package.json of the project:

> **Package Name:** This is the name of your package. It should be unique within the npm ecosystem. It's used to install the package via npm and to identify it in package listings.

> **Version:** This specifies the version of your package. It follows the semantic versioning format (major.minor.patch). It's used by npm to determine which versions of a package to install, among other things.

> **Description:** This is a brief description of what your package does. It helps users understand the purpose of your package when browsing package listings.

> **Entry Point:** Specifies the main entry point of your package, which is the starting point for your code when it's imported or required by other modules. This entry point is typically a JavaScript file that exports the main functionality of your package. When users install your package, this is the file that will be executed or imported first.

> **Test Command:** This is the command that should be run to execute your package's tests. It's typically used during development to ensure that your package behaves as expected.

> **Git Repository:** This is the URL of the Git repository where your package's source code is hosted. Including this information allows users to browse the source code and contribute to your project.

> **Keywords:** These are keywords or tags that describe your package. They help users discover your package when searching npm's package registry.

> **Author:** This is the name (and optionally email) of the package's author. It's typically used to credit the creator of the package

> **License:** This specifies the license under which your package is distributed. It's important for users to know what rights they have when using your package.



then we have to install the Express library for creating the ⚡magic⚡

```Bash
npm install express --save
```

And the time arrive, time to code. **We have to create our "main" JS file** like we had specified in the Entry Point parameter in the previous step, **in my case, index.js** , and we will start by importing the express module to our file.

```JavaScript
const express = require('express');
const app = express();
```

After this line, we could access to all Express functions to create and develop our API, using the const 'app'. Ant the most import part of an API it is ROUTES of course, so we will create one.

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

And finally we have to specify the port for the application, in this case we will use port 3000

```JavaScript
app.listen(3000, () => {
    console.log('Server start')
})
```

Now we only have to execute the index.js with node command

```Bash
node index.js
```

And if all is alright, we will see the log in the terminal

Now we can use Postman to check our new and beautiful endpoint working

![Postman Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3c50qkcczqlqkrdjsfk.png)

And this all for now, we will see more in next posts 🔮

Full Code
https://github.com/davidzcode/express-basic-api