How to use the aws-serverless-express/middleware.eventContext function in aws-serverless-express

To help you get started, we’ve selected a few aws-serverless-express examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jpdillingham / SWoT / api / src / index.js View on Github external
const awsServerlessExpress = require('aws-serverless-express');  
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

const express = require('express');  
const cors = require('cors');
const bodyParser = require('body-parser'); 

const exercises = require('./controllers/exercises');
const routines = require('./controllers/routines');
const workouts = require('./controllers/workouts');
const workoutsHistory = require('./controllers/workoutsHistory');

const app = express();

app.use(awsServerlessExpressMiddleware.eventContext());
app.use(cors({ exposedHeaders: 'X-Total-Count' }));
app.use(bodyParser.json());  
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/exercises', exercises);
app.use('/routines', routines);
app.use('/workouts/history', workoutsHistory);
app.use('/workouts', workouts);

app.listen(3000, () => console.log('Listening on port 3000.')); // ignored by lambda

const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
github Vadorequest / serverless-with-next5-boilerplate / src / functions / server / app.js View on Github external
import {isHostedOnAWS} from "../../utils/aws";

// XXX next.dev enables HMR, which we don't want if not in development mode, or when we are on AWS's infrastructure
const nextAppConfig = {
  dev: !isHostedOnAWS() && process.env.NODE_ENV === 'development',
  // dir: process.env.NODE_ENV === 'development' ? path.resolve(__dirname, '../../../.next') : '.', // XXX Attempt to fix Next.js app in "development" stage, which failed. Used a proxy instead.
  // conf: require('../../../next.config.js')
};
const nextApp = next(nextAppConfig);

const nextProxy = nextApp.getRequestHandler(); // Requests handled by nextProxy will be handled by the Next.js app
const app = express();

app.use(compression()); // See https://github.com/expressjs/compression/issues/133
app.use(awsServerlessExpressMiddleware.eventContext());

// Routes
app.get('/ko', (req, res) => {
  console.log('req from', req.protocol + '://' + req.get('host') + req.originalUrl);
  res.json({
    status: 'ko'
  })
});

app.get('/event', (req, res) => {
  console.log('req from', req.protocol + '://' + req.get('host') + req.originalUrl);
  res.send(`<pre>${JSON.stringify(req.apiGateway.event, null, 2)}</pre>`)
});

app.get('/static/:filename', (req, res) =&gt; {
  console.log('req from', req.protocol + '://' + req.get('host') + req.originalUrl);
github jonatassaraiva / microservice-serverless-lambda-nodejs / src / service / index.js View on Github external
'use strict';

//
// exnternal modules
const express = require('express');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

//
// microservice
const microservice = express();
microservice.use(awsServerlessExpressMiddleware.eventContext());

//
// routes
const basePath = '/notes';
microservice.get(basePath, (req, res) => {
  res.status(200).send({ message: 'OK Deploy' });
});

microservice.post(basePath, (req, res) => {
  res.status(201).send({ message: 'OK' });
});

microservice.put(basePath, (req, res) => {
  res.status(200).send({ message: 'OK' });
});
github aws-samples / aws-mobile-react-native-starter / backend / lambdas / crud / app.js View on Github external
*
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
const express = require('express');
const bodyParser = require('body-parser');
const AWS = require('aws-sdk');
const uuid = require('node-uuid');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

// declare a new express app
const app = express();
app.use(awsServerlessExpressMiddleware.eventContext({ deleteHeaders: false }), bodyParser.json());

const PETS_TABLE_NAME = `${process.env.MOBILE_HUB_DYNAMIC_PREFIX}-pets`;

AWS.config.update({ region: process.env.REGION });

const UNAUTH = 'UNAUTH';

// The DocumentClient class allows us to interact with DynamoDB using normal objects.
// Documentation for the class is available here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html
const dynamoDb = new AWS.DynamoDB.DocumentClient();

app.get('/items/pets', (req, res) => {
  // performs a DynamoDB Query operation to extract all records for the cognitoIdentityId in the table
  dynamoDb.query({
    TableName: PETS_TABLE_NAME,
    KeyConditions: {
github jonatassaraiva / microservice-serverless-lambda-nodejs / src / handler.js View on Github external
'use strict';

//
// external modules
const awsServerlessExpress = require('aws-serverless-express');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

//
// microservice
const microservice = require('./microservice/api');
microservice.use(awsServerlessExpressMiddleware.eventContext());

//
// exposed microservice
const microserviceServer = awsServerlessExpress.createServer(microservice);
module.exports.microservice = (event, context) => awsServerlessExpress.proxy(microserviceServer, event, context);
github DefinitelyTyped / DefinitelyTyped / aws-serverless-express / aws-serverless-express-tests.ts View on Github external
import * as awsServerlessExpress from 'aws-serverless-express';
import * as express from 'express';
import { eventContext } from 'aws-serverless-express/middleware';

const app = express();
app.use(eventContext());

const server = awsServerlessExpress.createServer(app, () => {});

const mockEvent = {
    key: 'value'
};

const mockContext = {
    callbackWaitsForEmptyEventLoop: true,
    functionName: 'testFunction',
    functionVersion: '1',
    invokedFunctionArn: 'arn',
    memoryLimitInMB: 128,
    awsRequestId: 'id',
    logGroupName: 'group',
    logStreamName: 'stream',
github mya-ake / vuejs-pwa-and-ssr-on-lambda / server / app.js View on Github external
'use strict';

const awsServerlessExpress = require('aws-serverless-express');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

const { app } = require('./server');
app.use(awsServerlessExpressMiddleware.eventContext());

const server = awsServerlessExpress.createServer(app);

module.exports.render = (event, context, callback) => {
  console.log(event);
  awsServerlessExpress.proxy(server, event, context);
};
github magishift / magishift.core / packages / keycloak / src / handler.ts View on Github external
async function bootstrapServer(): Promise {
  if (!cachedServer) {
    try {
      const expressApp = require('express')();

      const adapter = new ExpressAdapter(expressApp);

      const nestApp = await NestFactory.create(KeycloakModule, adapter);

      nestApp.use(eventContext());
      await nestApp.init();

      cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
    } catch (error) {
      return Promise.reject(error);
    }
  }
  return Promise.resolve(cachedServer);
}
github theodo / nestjs-serverless-demo / src / lambda.ts View on Github external
async function bootstrapServer(): Promise {
 if (!cachedServer) {
    const expressApp = express();
    const nestApp = await NestFactory.create(AppModule, new ExpressAdapter(expressApp))
    nestApp.use(eventContext());
    await nestApp.init();
    cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
 }
 return cachedServer;
}
github JimLynchCodes / Titanium-Lambda / app.sw-api-example.js View on Github external
'use strict';
/**
 *  @author Jim Lynch
 *
 *  The name of this file and object is exposes is "app". It is common in express.js examples to refer to the webserver
 *  being created as "app". This way we can handle our endpoints in a very similar way we do with non-servereless
 *  development.
 */
const express = require('express');
const app = express();
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const StarWarsFunctions = require('./src/star-wars-functions');
app.use(awsServerlessExpressMiddleware.eventContext())
app.starWarsFunctions = new StarWarsFunctions();

app.getHandler = function (req, res) {

  app.lambdaParams = {};

  if (req.apiGateway) {
    app.lambdaParams = Object.assign({}, app.lambdaParams, req.apiGateway.event)
  }

  if (req.query) {
    app.lambdaParams = Object.assign({}, app.lambdaParams, req.query)
  }

  res.set({
    'Content-Type': 'application/json',

aws-serverless-express

This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using your existing Node.js application framework.

Apache-2.0
Latest version published 4 years ago

Package Health Score

74 / 100
Full package analysis