Now that your API is live, let’s test it out. You’ll walk through a quick explanation of the code before testing one of your endpoints.
The web application is built using Express.js, a popular Node.js web application framework. You don’t need to use an existing web framework like Express.js when building applications on AWS Lambda, but it can ease the learning curve if you have experience with Express.
The core of the Express application is located in application/app.js. Open the file in the file explorer.
// application/app.js
const express = require('express')
const bodyParser = require('body-parser')
const { fetchUserScores, addUserScore, fetchTopScores } = require('./data')
const { createCognitoUser, login, verifyToken } = require('./auth')
const { validateCreateScore } = require('./validate')
const app = express()
app.use(bodyParser.json())
function wrapAsync(fn) {
return function(req, res, next) {
fn(req, res, next).catch(next);
};
}
// Login
app.post('/login', wrapAsync(async (req, res) => {
const idToken = await login(req.body.username, req.body.password)
res.json({ idToken })
}))
// Create user
app.post('/users', wrapAsync(async (req, res) => {
await createCognitoUser(req.body.username, req.body.password, req.body.email)
res.json({ username: req.body.username })
}))
// Fetch user scores
app.get('/users/:username', wrapAsync(async (req, res) => {
const limit = req.query.limit || 10;
const scores = await fetchUserScores(req.params.username, limit)
res.json(scores)
}))
// Add new score
app.post('/users/:username', wrapAsync(async (req, res) => {
const validated = validateCreateScore(req.body)
if (!validated.valid) {
throw new Error(validated.message)
}
const token = await verifyToken(req.header('Authorization'))
if (token['cognito:username'] != req.params.username) {
throw new Error('Unauthorized')
}
const score = await addUserScore(req.params.username, req.body.level, req.body.score)
res.json(score)
}))
// Fetch top scores
app.get('/scores/:date', wrapAsync(async (req, res) => {
const scores = await fetchTopScores(req.params.date)
res.json(scores)
}))
app.use(function(error, req, res, next) {
res.status(400).json({ message: error.message });
});
module.exports = app
At the top of this file, you import express and other dependencies, including the authentication helper functions and data access functions that you reviewed in previous modules. Then, you configure the various routes you want in your function, such as /login for a user to login and fetch an ID token or /users/:username to fetch the top scores for a particular user. Finally, at the bottom of the file, the script exports the Express application.
Next, look at application/handler.js. This is the file with the entrypoint method that you tell Lambda to invoke on an incoming request. The contents of this file are as follows:
// application/handler.js
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
This handler is using the aws-serverless-express package. This package converts an incoming API Gateway request into the standard request expected by the Express.js framework. This makes it easy to run Express.js code using Lambda and API Gateway.
Let’s test your endpoint using the same fetchUser function you saw in an earlier module. This time you go through your web application rather than calling the function directly.
Run the following command in your terminal:
You should see the following output in your terminal:
[{"game_id":101,"username":"ubecker","gamedate":"2019-11-06 09:00:37","score":9090,"level":84},{"game_id":148,"username":"ubecker","gamedate":"2019-11-10 07:53:27","score":8428,"level":30},{"game_id":146,"username":"ubecker","gamedate":"2019-11-06 13:28:49","score":8052,"level":86},{"game_id":33,"username":"ubecker","gamedate":"2019-11-08 08:05:11","score":7218,"level":18},{"game_id":5,"username":"ubecker","gamedate":"2019-11-07 17:56:25","score":6983,"level":91},{"game_id":252,"username":"ubecker","gamedate":"2019-11-05 18:57:45","score":5403,"level":8},{"game_id":245,"username":"ubecker","gamedate":"2019-11-10 06:16:58","score":5230,"level":75},{"game_id":51,"username":"ubecker","gamedate":"2019-11-07 10:44:46","score":5043,"level":2},{"game_id":282,"username":"ubecker","gamedate":"2019-11-07 02:58:57","score":4884,"level":17},{"game_id":106,"username":"ubecker","gamedate":"2019-11-07 07:56:40","score":4394,"level":17}]
Success! You hit your API endpoint. This triggered your Lambda function, which fetched the top scores for a user from your Amazon Aurora Serverless database using the Data API.