Building a professional API with NodeJS, Typescript, ExpressJS, MongoDB, Jest + Deploy to Heroku — PART 1

Withney Guilherme
2 min readFeb 19, 2022

--

API with Express, NodeJS PART 1

Guide:

Part 1 (you are here): Starting the project and set up a minimal NodeJS REST API
Part 2: Organizing the project, adding routes, controllers and isolated server
Part 3: Add typescript support + refactoring all javascript code to typescript and ES6
Part 4: Learn CRUD paradigm and implements it in NodeJS and test with CURL

Briefing

In this article, we will start to build a professional REST API with Typescript, Node.js, Express, MongoDB and then, deploy it to HEROKU. In the end, you can use your project as Boilerplate as a backend starter in the majority of your next projects.

What you will learn?

As mentioned before, in this article, you will start from scratch, then you will go through all the steps needed to build a complete Rest API. The API that you will build will be deployed to HEROKU as a production version and you will be apt to use it in real-world projects with the same method that we’ll do here.

Starting the project

Create a new dir to init your new NodeJS project.

mkdir node-api-rest
cd node-api-rest
Now start a node:
yarn init -y

Now your dir may have a package.json file. We’ll use the express package to start.

yarn add express 

In the application root, create a new folder called src and inside it, create the file app.js

mkdir src
touch src/app.js

Coding a minimal server

In the app.js let import the express and set up a minimal server response.

const express = require('express')
const app = express()
app.listen(3000)

And with only three lines, we already have our server running. But before the test, let’s add a response message with console.log, to let you see the message when the server started successfully.

const express = require('express')
const app = express()
app.listen(3000, ()=>console.log("Server is running at PORT 3000 🚀"))

Creating routes

In app.js, let’s create our route for HTTP requests.

const express = require('express')
const app = express()
// server application
app.listen(3000, ()=>console.log("Server is running at PORT 3000 🚀"))
// routes
app.get('/api',(req,res)=>{
res.send('API online')
})

We’ve created a GET route, which means when our API receives a GET HTTP request, the client will receive the res.send response.

By default, every browser sends a GET request when you access some address. So, for testing, restart your application in the terminal and access in your browser the address:

http://localhost:3000

Great! In this post, we saw how to code and run a simple API server and created a route for request/response.

In the next steps, we are going to organize our project and create controllers, routes, schemas, add typescript support, connect to MongoDB, put in JWT authentication, error handling, and a few more things, so stay tuned!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Withney Guilherme
Withney Guilherme

Written by Withney Guilherme

Tech Lead and Full Stack Developer NodeJS | React

No responses yet

Write a response