POSTING to Your Express Server

“Postman Icon” by Papirus Development Team is licensed under the GNU General Public License v3.0

While GET requests are fun, they’re not very practical for sending data to a server. Here, I’ll show you how to add a POST request endpoint to your server, and how to use the Postman software to test it!

For the sake of simplicity, we will not be using a database. Instead, we will store what we send to our server in an array.

Note: I will be assuming that you are familiar with Node and Express basics (Starting the server), as well as Path Parameters and Query Strings.


Overview

My Story “Running a Server Locally with Node and Express” left off with a “Hello world” GET request, and the addition of the “dogs” path.

Here, we’ll continue with the “dogs” theme to practice adding the POST to our server. As I said earlier, we will not be using a database. This means that our data will only be stored for as long as the server is running.


Technologies

Before we start coding, we need to make sure we have the proper technologies to run our server and send our HTTP requests (If you read my previous tutorial on starting a Node/Express server, you can skip to Postman).

Text Editors and Terminals

My personal choice of editor is Visual Studio Code, and the command line I use is Git Bash for Windows. Of course, you can use any text editor and terminal combination you want, these technologies just make my life easier.

Postman

In this tutorial, I’ll be using Postman to send requests to our Node/Express server.

Postman is a free desktop software that allows you to send HTTP requests. We need it in order to test POST requests.

Note: For those of you who are curious, there are other options besides Postman, such as the Advanced Rest Client, or using a form submission in html.


Getting Ready to Write

Open Your Coding Environment

Fig. 1, The code written in my previous Express tutorial on Starting a Server

If you followed my last Node/Express tutorial, and have code like in Fig. 1, you can just open up the project you used for that and skip to Installing the Dependencies.


If you don’t have a project yet, go ahead and make one!

Navigate to whatever directory you want your project to be in, and run npm init. This will create a new project. Click through the prompts that it gives you (Except the Entry Point, I prefer to change that to app.js).

You can find the necessary code for running the server and calling a GET request in my GitHub.

Once you have all the code you need, go back to the terminal and run npm install in the same directory as app.js. This will install all the necessary JavaScript libraries for the basic GET request server.

In the same directory as app.js, run node app.js in the terminal. If you copied my code, you should see a log that says: Server is listening on port 3000…

Open up your browser and go to http://localhost:3000, you should see text saying “Hello World!”, like in Fig. 2.

Fig. 2, What you should see when you open the root URL your server is running on

Installing the Dependencies

We’re going to need another JavaScript library in order to read data from POST requests on our server. Go to the terminal (in the same directory as app.js)and run:

npm install body-parser

Once that’s installed, go to the top of the app.js file where you have your import and instantiation for express:

The const express = require(‘express’) followed by app = express()

Below app = express(), put:

const bodyParser = require('body-parser'); app.use(bodyParser.json()); 

The require imports the body-parser library, and the app.use() tells our server (app), to use it to parse the data of requests!


Writing the code

Now that your server is up and we have all the dependencies we need, it’s time to start writing our code.

Adding a POST Request Endpoint

It’s time to write the endpoint for our POST request. This type of request allows you to send a chunk of data to a server. This contrasts with a GET request, which is used to retrieve data from a server.

First, since we’re not using a database, create an array to store your data at the top of your app.js file, below your imports, like so:

var dogsArr = [];

This is where we will store the data from our POST requests.

Now, scroll down to below the /dogs GET request and type in:

app.post('/dogs', function(req, res) {
var dog = req.body;
console.log(dog);
dogsArr.push(dog);
res.send("Dog added!");
});

This is going to be our POST endpoint for the /dogs route.

The “dog” data sent to the server is stored in the req.body (req standing for request), which is why we are assigning our dog variable its value. We then log the dog data in the console to check that we actually did receive the data, and then push the dog data into our dogsArr!


Testing The Code

It’s good practice to test our code after each added feature, so let’s test our new POST endpoint!

Start your server by running node app.js in the same directory that app.js is in.

Note: If you’ve already started your server before you added the POST request for dogs, you’ll need to restart it. In the terminal, press CTRL-C to stop the server, and then run node app.js to start it again.

Get Postman Ready

Go ahead and open up your Postman application. You’ll come to a window like in Fig. 2.

Fig. 2, The first screen you see when you open Postman

Exit out of it, and you’ll come to the main screen (Fig. 3):

Fig. 3, The main screen in Postman, used to create and send requests
Fig. 4, The drop-down from which you select the type of request you want to use

Change the type of request from GET to POST by selecting POST from the drop down menu (Fig. 4).

Then, enter the request url into the box next to it

(Our server is running at http://localhost:3000)

To access your POST dogs endpoint on the server, append /dogs to the end of the url. This will tell our server to execute the route:

app.post(‘/dogs’, function(req, res) {


Give Your Request a Body

Now we need to give our request a body (which holds the data of the request), since a POST request is used to send data to a server.

Do this by selecting body beneath the request URL. Make sure to check the raw radio button, and select JSON(application/json) from the orange drop-down on the right (Fig. 5).

Fig. 5, Select the “Body” option, then click the “raw” button to say that you want to send text, and then select JSON(application/json) from the orange drop down

Now, in the text area, enter some data in a format like this:

{
"name": "charlie",
"breed": "golden retriever"
}

This is your JSON request body, which will be sent to the server and parsed by the line var dog = req.body;


Send the Request!

Hit the blue Send button on the right (Fig. 6)!

Fig. 6, Send the request to your running express server

If you successfully receive the request, the body will be printed out by the console.log(dog) inside our POST.

You should also see a response at the bottom of the Postman window saying Dog added! This is sent by res.send("Dog added!") when the request completes.

Congratulations! You’ve successfully created a POST request endpoint on your Express server!

In addition to POST, there are other methods that can send data to a server, such as PUT and PATCH. These are used to modify data that already exists on the server.

To learn more about these requests, along with the DELETE HTTP request, see the below link!

Thanks for reading!