Express is a Node.js web application framework for routing HTTP requests and composing middleware. A typical application creates an Express app, registers middleware, defines routes, and listens on a port. section 16.3.2
An Express application can expose routes for an image-processing service:
- POST /image creates an image process and returns its identifier and initial state. section 16.3.2
- GET /image/:id retrieves the process state using a path parameter. section 16.3.2
- POST /image/:id/upload accepts an image upload for the selected process. section 16.3.2
Imagery web service
EC2 instances run Node.js and Express behind a load balancer. Express routes coordinate DynamoDB for process state, S3 for image storage, and SQS for worker processing. section 16.3.2
Listing 16.1 Initializing the Imagery server (server/server.js)
const express = require('express'); #1 const bodyParser = require('body-parser'); const AWS = require('aws-sdk'); const { v4: uuidv4 } = require('uuid'); const multiparty = require('multiparty'); const db = new AWS.DynamoDB({}); #2 const sqs = new AWS.SQS({}); #3 const s3 = new AWS.S3({}); #4 const app = express(); #5 app.use(bodyParser.json()); #6 // [...] app.listen(process.env.PORT || 8080, function() { #7 console.log('Server started. Open http://localhost:' + (process.env.PORT || 8080) + ' with browser.'); }); #1 Loads the Node.js modules (dependencies) #2 Creates a DynamoDB endpoint #3 Creates an SQS endpoint #4 Creates an S3 endpoint #5 Creates an Express application #6 Tells Express to parse the request bodies #7 Starts Express on the port defined by the environment variable PORT, or defaults to 8080
Shows Express application initialization, JSON body parsing, AWS endpoints, and port binding. section 16.3.2
Listing 16.2 Creating an image process with POST /image
app.post('/image', function(request, response) { #1
const id = uuidv4(); #2
db.putItem({ #3
'Item': {
'id': {
'S': id #4
},
'version': {
'N': '0' #5
},
'created': {
'N': Date.now().toString() #6
},
'state': {
'S': 'created' #7
}
},
'TableName': 'imagery-image', #8
'ConditionExpression': 'attribute_not_exists(id)' #9
}, function(err, data) {
if (err) {
throw err;
} else {
response.json({'id': id, 'state': 'created'}); #10
}
});
});
Shows a minimal Express POST route that persists an image process and returns JSON. section 16.3.2



