Building a RESTful API and Deploying it on AWS OpenShift: A Comprehensive Guide
Did you know experience of building RESTful API is necessary for every developer. If you are new to development it is important to know how to build RESTful API for communication. This tutorial is designed to provide information for beginner level.
Step 1: Setting Up Your Development Environment
- Download and install Node.js from the official website.
- Create a New Project:
mkdir my-rest-api
cd my-rest-api
npm init -y
3. Install Express because it is a minimal and flexible Node.js web application framework.
npm install express
4. Create a file named myrestapp.js in your project directory.
#Set the express variable
const express = require('express');
#application object
const app = express();
#port address for clients to respond
const PORT = process.env.PORT || 3000;
#client access methods
#you can add more methods
app.get('/api/myproject', (req, res) => {
res.json({ message: 'ItsmyfirstRESTAPI!' });
});
Step 2: Containerizing Your Application
#Place thos code in text file
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
#The command to process your application file
CMD ["node", "myrestapp.js"]
Build and run run your docker container locally.
docker build -t my-rest-api .
Step 3: OpenShift
- Use the OpenShift CLI (
oc
) to log in to your cluster. - Create a New Project.
#Developer Sandbox
oc new-project my-rest-api-project
3. Initialize a Git repository in your directory.
4. Go to the OpenShift web console.
5. Click on “+Add” and select “Import from Git”.
6. Enter your GitHub repository URL.
7. Configure Deployment Settings:
- Specify the path to your
Dockerfile
. - Set the target port (3000) under advanced options.
8. Click “Create” to start the deployment process.
9. Once deployed, you can access your API via the route provided by OpenShift.