Skip to content

Instantly share code, notes, and snippets.

@nvd
Created May 23, 2016 12:11
Show Gist options
  • Save nvd/ba1c0aebf989a6a8c63c0ccfa5cd034f to your computer and use it in GitHub Desktop.
Save nvd/ba1c0aebf989a6a8c63c0ccfa5cd034f to your computer and use it in GitHub Desktop.
Nodejs app to push/pop with SQS
//-------------------------------------
// Nodejs app to push/pop with SQS
// $ npm install aws-sdk express
// $ node sqs-express.js
//-------------------------------------
var express = require('express'),
AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET,
region: 'ap-southeast-2'});
var app = express(),
sqs = new AWS.SQS(),
queueUrl = 'https://sqs.ap-southeast-2.amazonaws.com/1234567890/my_test_queue';
//-------------------------------------
function logErrorOrData(action, err, data) {
console.log("------\t"+action+"\t-----------")
if (err)
console.log(err, err.stack);
else
console.log(data);
console.log("---------------------------")
}
function deleteMessage(handle) {
var params = {
QueueUrl: queueUrl,
ReceiptHandle: handle
};
sqs.deleteMessage(params, function(err, data) {
logErrorOrData('delete', err, data)
});
}
//----------- ROUTES ------------------
app.get('/push', function(req, res) {
var params = {
QueueUrl: queueUrl,
MessageBody: JSON.stringify(req.query),
};
sqs.sendMessage(params, function(err, data) {
logErrorOrData('send', err, data);
});
res.send('OK');
});
app.get('/pop', function(req, res) {
var params = {
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
};
sqs.receiveMessage(params, function(err, data) {
logErrorOrData('receive', err, data);
if (!err) deleteMessage(data.Messages[0]['ReceiptHandle']);
});
res.send('OK');
});
app.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment