DEV Community


Posted on

How to: Create an API Rest with Express - Basic

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

npm init
Enter fullscreen mode Exit fullscreen mode

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⚡

npm install express --save
Enter fullscreen mode Exit fullscreen mode

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.

const express = require('express');
const app = express();
Enter fullscreen mode Exit fullscreen mode

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.

app.get('/', (req, res) => {
  res.send('Hello World!');
Enter fullscreen mode Exit fullscreen mode

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

app.listen(3000, () => {
    console.log('Server start')
Enter fullscreen mode Exit fullscreen mode

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

node index.js
Enter fullscreen mode Exit fullscreen mode

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

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

Full Code

Top comments (0)