Contents
aws-lambdas-typescript-example is a small repository from 2023, and its name promises more than it holds. There is no Lambda function in it. The README title says "Node.js Express API's", and that is accurate: it is two TypeScript files that start an Express server.
The code is still a decent first look at how an Express app is wired in TypeScript, and its one real feature, a request-logging middleware, has a few lessons in it.
What the name says and what the code does
| Source | What it says | What the code shows |
|---|---|---|
| Repo name | AWS Lambdas | No handler, no AWS SDK, no serverless or SAM file |
| package.json name and keywords | aws-lambdas-api-using-typescript, aws-api-getways, lambdas | Metadata only, and nothing in src/ mentions Lambda or API Gateway |
| README title | Node.js Express API's | Correct: express is a dependency and app.listen runs it |
| README endpoints | / and /api | Only GET / exists in src/index.ts |
The README also calls the project a base architecture. It has two source files, so I would call it a starting point at most. The /api row is a README claim the code does not back, and I have left it out of everything below.
The server in a few lines
import express, { Request, Response } from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import { loggerMiddleware } from './api-logs';
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.json());
app.use(loggerMiddleware);
app.get('/', (request: Request, response: Response) => {
return response.json({ message: 'Hello World!' });
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});The port comes from PORT and falls back to 3000. The default imports (import express from 'express') work because tsconfig.json sets esModuleInterop. The dev script runs ts-node-dev src/index.ts, and build runs tsc into dist. There is no start script, so a built copy runs with node dist/index.js.
A Lambda function exports a handler that AWS invokes with an event. app.listen opens a port and waits, which is a different execution model. Running this app on Lambda would need an adapter from API Gateway events to Express requests, plus deployment config. None of that is here.
Why the order of use calls matters
Express runs middleware in the order it is registered. Here bodyParser.json() comes before loggerMiddleware, so request.body is already parsed when the logger reads it. The logger comes before the route, so it sees every request, including paths that match nothing.
The logging middleware
src/api-logs.ts prints a timestamp, the method and the path. An environment variable switches on a payload dump. Trimmed, with the banner lines shortened:
const SHOW_PAYLOAD =
typeof process.env.SHOW_PAYLOAD === 'undefined'
? false
: JSON.parse(process.env.SHOW_PAYLOAD)
if (SHOW_PAYLOAD) {
if (request.headers) {
payload += `\n---REQUEST HEADERS---\n${JSON.stringify(request.headers, null, 4)}`
}
// query and body are handled the same way
}
console.log(`${date} - ${time} - ${request.method} ${request.path} ${payload}`);
next();The date and time are formatted with the en-US locale. SHOW_PAYLOAD is off unless it is set, which is the right default for a payload logger.
Three things I would fix
- Parse the flag once.
JSON.parsethrows on anything that is not valid JSON, such asyesorTRUE. The call sits inside the middleware, so with such a value every request hits the exception and Express answers it with a 500. Reading the variable at module load makes a bad value fail at startup. - Do not log all headers. With the flag on,
Authorizationand cookie headers go to standard output, and a request body can hold passwords. - Log when the response ends. The middleware calls
next()straight after printing, so the log line cannot show a status code or a duration. Listening for the response'sfinishevent would.
What this repo is good for
It is useful as a minimal Express and TypeScript skeleton, and as a small example of a middleware that reads a flag. It is not a Lambda example, and .gitignore lists package-lock.json, so installs are not pinned. The ^ ranges in package.json can move, and tsconfig.json does not turn on strict.
For a Lambda project I would start from a handler and a deployment definition, then add Express only if the routing earns its weight. For a plain Express API I would keep this layout, enable strict, and fix the three items above first.
Found this useful? Share it with your team.