Another ExpressJS API tutorial for 2021, part 01 — hello world

Marcos Henrique da Silva
4 min readMar 13, 2020

Hey there, here I am proposing some short written tutorials on how to build an API using ExpressJS in our 2021.

Great title right? Probably no,

BUT, since you are curious or even new to Node.JS universe and like more hands-on tutorials and learning by using, this might be a good series of articles for you.

This is also a not complete level 0 beginners tutorial, it would be nice for you to understand a Javascript, Typescript and the basics of Node.JS framework.

What would you be learning and practicing on these series of small articles?

So, it will be 12 publications, each one with a very short and specific topic that will be using the previous publication as base.

Should we start?

Please make sure to have Node.JS installed. Create a project folder and on your terminal type npm init . You can fill all information asked as the way you please. Or, create directly a package.json and copy and paste the following:

{
"name": "expressjs-api-tutorial",
"version": "0.0.1",
"description": "Tutorial of how to create a REST API using ExpressJS",
"main": "dist/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/makinhs/expressjs-api-tutorial.git"
},
"keywords": [
"REST",
"API",
"ExpressJS",
"NodeJS"
],
"author": "Marcos Silva",
"license": "ISC",
"bugs": {
"url": "https://github.com/makinhs/expressjs-api-tutorial/issues"
},
"homepage": "https://github.com/makinhs/expressjs-api-tutorial#readme",
"dependencies": {
"express": "^4.17.1"
}
}

The URL above contains our project, which you can clone/fork using Github, and also take a look with the full coded project with all the future publications as a code and ready to use. If you didn’t copied and pasted, please run at your terminal npm i --save express before continuing this article.

In this article we are focusing on the current Github branch:

https://github.com/makinhs/expressjs-api-tutorial/tree/001-criando-o-projeto (don't worry about these not English branch name, everything else is in English ;) )

Now that you have your package.json settled, at your terminal run npm install

To let the project with a minimal structure, let’s create an app folder and create an index.js file inside of it. Now let’s code a bit:

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const port = 3000;
app.get('/', (req, res) => {
res.status(200).send(`Hello World! Our server is running at port ${port}`);
});
server.listen(port, () => {
console.log(`Server running at port ${port}`);
});

Here we are starting our express application when we invoke express() function to our app field. To make it work we use the Node.JS http module and we create a server passing inside our app.

Our first route is a get to / which would be like localhost:3000 that you can try on your browser which will only have a response that the server is running.

To start the server we need to call .listen passing the port we want to listen and a function to be called after the server is started.

That’s it, we have our first baby steps to create an API using ExpressJS.

At the next publication we will change our project to Typescript and things will start to get more fun.

Thanks for reading and see you at the next article!

tips:

As an Amazon Associate I earn from qualifying purchases. That is why I recommend here some paid links that matches with the article that I am writing and might be useful for your studies! Buying from this links you motivate me to keep writing and will learn better coding

--

--