Basics of Creating Web APIs with Node.JS and Express
Fast, unopinionated, minimalist web framework for Node.js
Express is a web application framework built on node.js. This post will explain the basics of setting it up and creating a simple web application.
Setup
Let’s start this series of posts by creating a basic app that will send back a predetermined string to the web browser. First, create a new directory and cd into it.
mkdir hello-world && cd hello-world
Once there, we need to create our package.json to store the required modules for our application and store basic information about the project. We’ll run the following npm init -y which will run through the process without asking us any additional questions; if you’d like the more verbose method, remove -y from the command. We now have a package.json that looks like this:
/* ./package.json */
{
"name": "hello-world",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
} Let’s run through this line by line quickly.
- “name” is the project name, which is just grabbed directly from the folder name.
- “version” is what version of application this is currently.
- “description” is where we put a short description of the application.
- “main” is the filename of the main application file. By default, this is set to index.js.
- “scripts” are where we can add things to run by typing “npm *“.
- “keywords” takes an array of strings and is used to help people find your package when uploaded to npm.
- “author” is the name of the person creating the app.
- “license” whatever license you’re releasing under. defaults to ISC
Next up, let us install express with npm so we can access it in our application. In the hello-world directory, type the following in terminal npm i express. This will add express to our package.json under a new section called “dependencies.” And we now have access to express and can use it in our application. Let’s start by creating our index.js and filling it with a basic application to send a message to the browser when hitting the root path.
// index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('root path');
});
app.listen(3000, () => {
console.log('listening on port 3000');
}); The Rundown
- First, we import the express module and assign it to a
constsince this won’t change. - We then create a
const appthat calls express and gives us access to all the modules we’ll need. app.get(...tells express to watch for a GET request on the given path, which is the first argument it takes'/'in our case. The second argument is the callback function that tells the server to send the message from/back to the user once the path has been hit.- We then start up the server on port 3000 and return a console message, so we know it’s running.
That’s it for this portion of my express series. We’ve created a primary server that sends a message back to us when we hit the '/' path on our localhost. Easy Peasy. Next, we’ll get into view rendering and some different types of engines we can use.