• Latest
Building a RESTful API With AWS Lambda and Express

Building a RESTful API With AWS Lambda and Express

March 15, 2023
Etrian Odyssey Origins Collection Review (Switch eShop)

Etrian Odyssey Origins Collection Review (Switch eShop)

June 1, 2023
Cosmic Mining’, ‘Squish Run’, ‘Brainiversity’ and More – TouchArcade

Cosmic Mining’, ‘Squish Run’, ‘Brainiversity’ and More – TouchArcade

June 1, 2023
😎😎😎😎😎😎😉10000000

😎😎😎😎😎😎😉10000000

June 1, 2023
Dragon Ball: The Breakers Season 3 Adds The Mighty Ginyu Force As Raiders

Dragon Ball: The Breakers Season 3 Adds The Mighty Ginyu Force As Raiders

June 1, 2023
Natural Star 🎥|| 💥💥#shorts #trending #viral #short #youtubeshorts

Natural Star 🎥|| 💥💥#shorts #trending #viral #short #youtubeshorts

June 1, 2023
Feel #kedarnath baba Blessing & #dance 💥💥#shorts #short #viral

Feel #kedarnath baba Blessing & #dance 💥💥#shorts #short #viral

June 1, 2023
playing spiderman game in playstation 5 ps 5 explaining how to play game in laptop or in computer

playing spiderman game in playstation 5 ps 5 explaining how to play game in laptop or in computer

June 1, 2023
FORTNITE AND MARQUES BROWNLEE

FORTNITE AND MARQUES BROWNLEE

June 1, 2023
⚡⚡Technical Guruji, Marques Brownlee & Unbox Tharepy 🔥 Who is the biggest Tech Youtuber #shorts 🔥

⚡⚡Technical Guruji, Marques Brownlee & Unbox Tharepy 🔥 Who is the biggest Tech Youtuber #shorts 🔥

June 1, 2023
No building? Marques Brownlee? || Fortnite || CSGO || #StandWithUkraine

No building? Marques Brownlee? || Fortnite || CSGO || #StandWithUkraine

June 1, 2023
@Marques Brownlee   #Green bubbles vs blue bubbles

@Marques Brownlee #Green bubbles vs blue bubbles

June 1, 2023
Disney Dreamlight Valley Reveals “Updated 2023 Content Roadmap”

Disney Dreamlight Valley Reveals “Updated 2023 Content Roadmap”

June 1, 2023
Advertise with us
Thursday, June 1, 2023
Bookmarks
  • Login
  • Register
GetUpdated
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

Building a RESTful API With AWS Lambda and Express

March 15, 2023
in Software Development
Reading Time:5 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Together, AWS Lambda and Node.js can be used to create a RESTful API that can be triggered by events such as an HTTP request.

Prerequisites

Before building a RESTful API with Express.js, you should have the following in place:

  1. As Express.js is a JavaScript framework, you’ll need Node.js installed to run it. You can download Node.js from the official website.
  2. Text Editor or Integrated Development Environment (IDE): To write and edit your API code, you will need a text editor or IDE. Examples of popular text editors are Sublime Text and Visual Studio Code, while popular IDEs are WebStorm and Visual Studio.
  3. In order to write your API, you should have a basic understanding of JavaScript, since Express.js is written in JavaScript.
  4. A familiarity with Express.js: Express.js is a web framework for Node.js that helps you build web applications and APIs quickly and easily.
  5. The RESTful APIs use HTTP for communication, so you should be familiar with the protocol. It is necessary to have a basic understanding of HTTP methods (GET, POST, PUT, DELETE) and their intended uses, status codes, and the format of HTTP requests and responses.
  6. For keeping track of changes to your codebase, familiarity with version control systems (VCS) like Git is helpful.

As soon as you have these prerequisites in place, you can start building your RESTful API with Express.js.

Adding in The Code

1. Create an AWS_Node folder using the mkdir command in the terminal and cd into the directory.

mkdir aws-lambda-express-demo
cd aws-lambda-express-demo

2. Create an app.js file in your AWS_Node folder.

touch app.js

Libraries

1. We’ll use npm to download the latest version of the express package from the npm registry and store it in the node_modules folder in your project’s root directory. The package’s dependencies will also be installed and stored there as well.

npm install express

2. Next we’ll install a middleware-compatible framework called serverless-http, a library for creating serverless applications. AWS Lambda allows you to write your application normally, and then wrap it around a function that is exported and executed using an HTTP request. Aside from Azure, Google Cloud, and others, it is also compatible with serverless providers.

npm install serverless-http

You can install the package globally by running npm install -g serverless-http. 

Here’s an example of a RESTful API implemented using Node.js with the Express.js framework that implements the GET, POST, DELETE, and PUT methods:

This code creates an Express.js app and adds routes for the GET, POST, DELETE, and PUT methods. It uses an in-memory items array to store data and uses the find and findIndex methods to retrieve and update items based on the id provided in the URL. Note that for the POST and PUT routes, you will have to parse the body of the request, which you can do with middleware such as body-parser.

const express = require('express');
const app = express();
const serverless = require('serverless-http');
const users = [
    { id: 1, name: 'John', company: "ABC Company" },
    { id: 2, name: 'Frank', company: "XYZ Inc." },
    { id: 3, name: 'Ashley', company: "123 Company" },
];app.get('/users', (req, res) => {
    res.json(users);
});app.get('/users/:id', (req, res) => {
    const user = users.find(user => user.id === parseInt(req.params.id));
    if (!user) res.status(404).json({ message: 'User not found' });
    res.json(user);
});app.post('/users', (req, res) => {
    const user = {
        id: users.length + 1,
        name: req.body.name,
        company: req.body.company,
    };
    users.push(user);
    res.json(user);
});app.delete('/users/:id', (req, res) => {
    const userIndex = users.findIndex(user => user.id === parseInt(req.params.id));
    if (userIndex === -1) res.status(404).json({ message: 'User not found' });
    users.splice(userIndex, 1);
    res.json({ message: 'User deleted' });
});app.put('/users/:id', (req, res) => {
    let user = users.find(user => user.id === parseInt(req.params.id));
    if (!user) res.status(404).json({ message: 'User not found' });
    user.name = req.body.name;
    user.company = req.body.company;
    res.json(user);
});
const handler = serverless(app);const startServer = async () => {
    app.listen(3000, () => {
      console.log("listening on port 3000!");
    });
}startServer();module.exports.handler = (event, context, callback) => {
    const response = handler(event, context, callback);
    return response;
};

We’ll write the line console.log("listening on port 3000!"); to indicate that your API is up and running. Finally, module.exports.handler is a function that takes in event, context, and callback arguments, and calls the handler function passing in the event, context, and callback arguments.

Running and Testing the Code

Start the server by running the following:

node app.js

Now, our API is up and running. You can send a test HTTP request through Postman. By sending a request to localhost:3000/users, you should see a 200 OK status code. For this test, no request body is needed for the incoming request.

Test HTTP request through Postman



Source link

ShareSendTweet
Previous Post

Marques Brownlee #shorts

Next Post

World Of Warcraft Classic Announces Return Of Level 55 Death Knight Character Creation Restriction

Related Posts

Decoding eBPF Observability: How eBPF Transforms Observability as We Know It

June 1, 2023
0
0
Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
Software Development

There has been a lot of chatter about eBPF in cloud-native communities over the last 2 years. eBPF was a...

Read more

Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris

June 1, 2023
0
0
Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
Software Development

I worked as a real-time computing engineer for a due diligence platform, which is designed to allow users to search...

Read more
Next Post
World Of Warcraft Classic Announces Return Of Level 55 Death Knight Character Creation Restriction

World Of Warcraft Classic Announces Return Of Level 55 Death Knight Character Creation Restriction

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?