-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (53 loc) · 1.67 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Include the cluster module
const cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
const cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for terminating workers
cluster.on('exit', function (worker) {
// Replace the terminated workers
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
const AWS = require('aws-sdk');
const express = require('express');
AWS.config.region = process.env.REGION
const ddb = new AWS.DynamoDB();
const productsTable = process.env.PRODUCTS_TABLE;
const app = express();
app.get('/', function(req, res) {
// get a sample item from the products table
ddb.getItem({
TableName: productsTable,
Key: {
id: {
S: 'banana'
}
}
}, function(err, data) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
} else {
// and return it if it exists
return res.status(200).json({
message: 'Success',
obj: data
});
}
});
});
const port = process.env.PORT || 3000;
const server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
}