Creating a Basic Express.js API on AWS EC2 (Linux)

Posted by






AWS EC2: A simple Express.js API (Linux)

AWS EC2: A simple Express.js API (Linux)

AWS EC2 is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. In this article, we will create a simple Express.js API on an AWS EC2 instance running on Linux.

Step 1: Launch an EC2 instance

First, log in to the AWS Management Console and navigate to the EC2 dashboard. Click on the “Launch Instance” button and choose an Amazon Machine Image (AMI) that is compatible with Express.js and supports Linux. Select an instance type, configure the instance details, add storage, and configure security groups. Finally, launch the instance and create a key pair to access the instance.

Step 2: Connect to the EC2 instance

Once the EC2 instance is launched, connect to it using an SSH client. Use the key pair generated during the instance launch process to access the instance. Once connected, update the package repository and install Node.js and npm using the package manager.

“`
ssh -i /path/to/keypair.pem ec2-user@
sudo yum update -y
sudo yum install -y nodejs
sudo yum install -y npm
“`

Step 3: Create an Express.js API

Create a new directory for the Express.js application and navigate to it. Initialize a new Node.js project and install Express.js as a dependency using npm. Create a new file for the Express.js API and define a simple route that returns a “Hello, World!” message.

“`
mkdir express-api
cd express-api
npm init -y
npm install express –save
touch app.js
“`

app.js
“`javascript
const express = require(‘express’);
const app = express();

app.get(‘/’, (req, res) => {
res.send(‘Hello, World!’);
});

app.listen(3000, () => {
console.log(‘Express.js API running on port 3000’);
});
“`

Step 4: Run the Express.js API

Run the Express.js API on the EC2 instance using Node.js. Start the API and verify that it is running correctly by accessing the public IP address of the EC2 instance in a web browser.

“`
node app.js
“`

Conclusion

In this article, we have created a simple Express.js API on an AWS EC2 instance running on Linux. We have launched an EC2 instance, connected to it, installed Node.js and npm, created an Express.js API, and run the API on the EC2 instance. This demonstrates the basic steps to set up a web server on AWS using EC2 and Node.js.


0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Em Dwyer
11 months ago

AMAZING WORK