avatarMarcos Henrique da Silva

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3750

Abstract

, <span class="hljs-string">"description"</span>: <span class="hljs-string">"Tutorial of how to create a REST API using ExpressJS"</span>, <span class="hljs-string">"main"</span>: <span class="hljs-string">"dist/app.js"</span>, <span class="hljs-string">"scripts"</span>: { <span class="hljs-string">"test"</span>: <span class="hljs-string">"echo "</span><span class="hljs-keyword">Error</span>: <span class="hljs-keyword">no</span> <span class="hljs-keyword">test</span> specified<span class="hljs-string">" && exit 1"</span> }, <span class="hljs-string">"repository"</span>: { <span class="hljs-string">"type"</span>: <span class="hljs-string">"git"</span>, <span class="hljs-string">"url"</span>: <span class="hljs-string">"git+https://github.com/makinhs/expressjs-api-tutorial.git"</span> }, <span class="hljs-string">"keywords"</span>: [ <span class="hljs-string">"REST"</span>, <span class="hljs-string">"API"</span>, <span class="hljs-string">"ExpressJS"</span>, <span class="hljs-string">"NodeJS"</span> ], <span class="hljs-string">"author"</span>: <span class="hljs-string">"Marcos Silva"</span>, <span class="hljs-string">"license"</span>: <span class="hljs-string">"ISC"</span>, <span class="hljs-string">"bugs"</span>: { <span class="hljs-string">"url"</span>: <span class="hljs-string">"https://github.com/makinhs/expressjs-api-tutorial/issues"</span> }, <span class="hljs-string">"homepage"</span>: <span class="hljs-string">"https://github.com/makinhs/expressjs-api-tutorial#readme"</span>, <span class="hljs-string">"dependencies"</span>: { <span class="hljs-string">"express"</span>: <span class="hljs-string">"^4.17.1"</span> } }</pre></div><p id="8fd5">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 <code>npm i --save express</code> before continuing this article.</p><p id="f3f4">In this article we are focusing on the current Github branch:</p><div id="7392"><pre><span class="hljs-symbol">https:</span><span class="hljs-comment">//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 ;) )</span></pre></div><p id="2b55">Now that you have your <code>package.json</code> settled, at your terminal run <code>npm install</code></p><p id="3c39">To let the project with a minimal structure, let’s create an <code>app</code> folder and create an <code>index.js</code> file inside of it. Now let’s code a bit:</p><div id="2cd2"><pre><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>); <span class="hljs-keyword">const</span> app = <span class="hljs-title function_">express</span>(); <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>); <span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>(app); <span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;</pre></div><div id="94de"><pre>app.<span class="hljs-built_in">get</span>(<span class="hljs-string">'/'</span>, (req, res) => { res.status(200).send(Hello World! Our<span class="hljs-built_in"> server </span>is running at<span class="hljs-built_in"> port </span><span class="hljs-variable">${port}</span>); });</pre></div><div id="ffd0"><pre>server.listen(port, () => { <span class="hljs-built_in">console</span>.<span class="hljs-buil

Options

t_in">log</span>(<span class="hljs-string">Server running at port <span class="hljs-subst">${port}</span></span>); });</pre></div><p id="a8ff">Here we are starting our express application when we invoke <code>express()</code> function to our <code>app</code> field. To make it work we use the Node.JS <code>http</code> module and we create a server passing inside our <code>app</code>.</p><p id="7aea">Our first route is a get to <code>/</code> which would be like <code>localhost:3000</code> that you can try on your browser which will only have a response that the server is running.</p><p id="49cf">To start the server we need to call <code>.listen</code> passing the port we want to listen and a function to be called after the server is started.</p><p id="abb1">That’s it, we have our first baby steps to create an <b>API</b> using <b>ExpressJS</b>.</p><p id="cda7">At the next publication we will change our project to <b>Typescript</b> and things will start to get more fun.</p><p id="bbba">Thanks for reading and see you at the <a href="https://readmedium.com/another-expressjs-api-tutorial-for-2020-part-02-going-to-typescript-1bb574c8d12b">next article</a>!</p><div id="1f50" class="link-block"> <a href="https://readmedium.com/another-expressjs-api-tutorial-for-2020-part-02-going-to-typescript-1bb574c8d12b"> <div> <div> <h2>Another ExpressJS API tutorial for 2020, part 02 — going to Typescript</h2> <div><h3>Starting an API ExpressJS project using Typescript</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*eWYm3Dm7s0t8lLWYRW4pow.jpeg)"></div> </div> </div> </a> </div><p id="2436">tips:</p><ul><li>Still didn’t know <b>ECMAScript</b>? Start at least here: <a href="http://es6-features.org/">http://es6-features.org/</a></li><li>Read <b>ExpressJS</b> documentation! <a href="https://expressjs.com/en/4x/api.html">https://expressjs.com/en/4x/api.html</a></li><li>Download <b>Node.JS</b> here: <a href="https://nodejs.org/en/">https://nodejs.org/</a></li><li>Book suggestion: <a href="https://amzn.to/2WGb1f5"><b>Design Patterns</b></a></li><li>Brazilian? PT-BR Book suggestion: <a href="https://amzn.to/2U6uXWI"><b>Design Patterns</b></a></li><li>Complete project is <a href="https://github.com/makinhs/expressjs-api-tutorial">here</a>!</li></ul><p id="d417">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</p><ul><li>(paid) Book Suggestion: <a href="https://amzn.to/36xdL3c">Clean Code</a></li></ul><p id="9aa9">Want to learn backend development with NestJS?</p><div id="bb5d" class="link-block"> <a href="https://makinhs.medium.com/creating-a-rest-api-series-with-nestjs-part-01-scaffolding-and-basic-cli-usage-30ace19c5bb8"> <div> <div> <h2>Creating a REST API series with NestJS — Part 01 — Scaffolding and basic CLI usage</h2> <div><h3>Welcome back! In the mini series of article I am going to guide you on how to built a REST API using NestJS framework</h3></div> <div><p>makinhs.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*ZkV2evZT885DbdOi4ulasA.jpeg)"></div> </div> </div> </a> </div></article></body>

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

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

Want to learn backend development with NestJS?

Expressjs
Nodejs
Nodejs Tutorial
API
Api Development
Recommended from ReadMedium