-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
759 additions
and
771 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
'use strict' | ||
'use strict'; | ||
|
||
const fs = require('fs') | ||
const awsServerlessExpress = require('aws-serverless-express') | ||
const fs = require('fs'); | ||
const awsServerlessExpress = require('aws-serverless-express'); | ||
|
||
exports.handler = async (event, context) => { | ||
// remove AWS Lambda default handling of unhandled rejections | ||
// this makes sure dev mode cli catches unhandled rejections | ||
process.removeAllListeners('unhandledRejection') | ||
process.removeAllListeners('unhandledRejection'); | ||
|
||
// make event object look like APIG 1.0 | ||
// until aws-serverless-express supports APIG 2.0 | ||
event.path = event.requestContext.http.path | ||
event.method = event.requestContext.http.method | ||
event.httpMethod = event.requestContext.http.method | ||
event.path = event.requestContext.http.path; | ||
event.method = event.requestContext.http.method; | ||
event.httpMethod = event.requestContext.http.method; | ||
// APIG 2.0 extracts cookies from headers automatically. | ||
// We need to put `cookie` back to `headers.cookie` so it works with current aws-serverless-express | ||
if (event.cookies && event.cookies.length > 0) { | ||
event.headers.cookie = event.cookies.join('; ') | ||
event.headers.cookie = event.cookies.join('; '); | ||
} | ||
|
||
// NOTICE: require() is relative to this file, while existsSync() is relative to the cwd, which is the root of lambda | ||
let app | ||
let app; | ||
if (fs.existsSync('./app.js')) { | ||
// load the user provided app | ||
|
||
try { | ||
// eslint-disable-next-line import/no-unresolved | ||
app = require('../app.js') | ||
app = require('../app.js'); | ||
} catch (e) { | ||
if (e.message.includes("Cannot find module 'express'")) { | ||
// user probably did not run "npm i". return a helpful message. | ||
return { | ||
statusCode: 404, | ||
body: | ||
'The "express" dependency was not found. Did you install "express" as a dependency within your source folder via npm?' | ||
} | ||
'The "express" dependency was not found. Did you install "express" as a dependency within your source folder via npm?', | ||
}; | ||
} | ||
// some other require error | ||
return { | ||
statusCode: 404, | ||
body: e.message | ||
} | ||
body: e.message, | ||
}; | ||
} | ||
} else { | ||
// load the built-in default app | ||
app = require('../_src/app.js') | ||
app = require('../_src/app.js'); | ||
} | ||
|
||
if (typeof app !== 'function') { | ||
// make sure user exported app in app.js or return a helpful message. | ||
return { | ||
statusCode: 404, | ||
body: | ||
'Express app not found. Please make sure you are exporting express from an "app.js" file.' | ||
} | ||
'Express app not found. Please make sure you are exporting express from an "app.js" file.', | ||
}; | ||
} | ||
|
||
const server = awsServerlessExpress.createServer(app) | ||
const res = await awsServerlessExpress.proxy(server, event, context, 'PROMISE') | ||
return res.promise | ||
} | ||
const server = awsServerlessExpress.createServer(app); | ||
const res = await awsServerlessExpress.proxy(server, event, context, 'PROMISE'); | ||
return res.promise; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
'use strict' | ||
'use strict'; | ||
|
||
const express = require('express') | ||
const express = require('express'); | ||
|
||
const app = express() | ||
const app = express(); | ||
|
||
// Routes | ||
app.get('/*', (req, res) => { | ||
res.send(`Request received: ${req.method} - ${req.path}`) | ||
}) | ||
res.send(`Request received: ${req.method} - ${req.path}`); | ||
}); | ||
|
||
// Error handler | ||
app.use((err, req, res) => { | ||
console.error(err) | ||
res.status(500).send('Internal Serverless Error') | ||
}) | ||
console.error(err); | ||
res.status(500).send('Internal Serverless Error'); | ||
}); | ||
|
||
module.exports = app | ||
module.exports = app; |
Oops, something went wrong.