This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from netlify/netlify-functions/templatesV2
add v2 of templates with assets and no requiring of all the things
- Loading branch information
Showing
9 changed files
with
238 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
this folder contains other folders that will be copied alongside the template. | ||
|
||
`/app/index.js` contains a plain HTML template that we return from serverless function |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Express App */ | ||
const express = require('express') | ||
const cors = require('cors') | ||
const morgan = require('morgan') | ||
const bodyParser = require('body-parser') | ||
const compression = require('compression') | ||
|
||
/* My express App */ | ||
module.exports = function expressApp(functionName) { | ||
const app = express() | ||
const router = express.Router() | ||
|
||
// gzip responses | ||
router.use(compression()) | ||
|
||
// Set router base path for local dev | ||
const routerBasePath = process.env.NODE_ENV === 'dev' ? `/${functionName}` : `/.netlify/functions/${functionName}/` | ||
|
||
/* define routes */ | ||
router.get('/', (req, res) => { | ||
const html = ` | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
padding: 30px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Express via '${functionName}' ⊂◉‿◉つ</h1> | ||
<p>I'm using Express running via a <a href='https://www.netlify.com/docs/functions/' target='_blank'>Netlify Function</a>.</p> | ||
<p>Choose a route:</p> | ||
<div> | ||
<a href='/.netlify/functions/${functionName}/users'>View /users route</a> | ||
</div> | ||
<div> | ||
<a href='/.netlify/functions/${functionName}/hello'>View /hello route</a> | ||
</div> | ||
<br/> | ||
<br/> | ||
<div> | ||
<a href='/'> | ||
Go back to demo homepage | ||
</a> | ||
</div> | ||
<br/> | ||
<br/> | ||
<div> | ||
<a href='https://github.com/DavidWells/netlify-functions-express' target='_blank'> | ||
See the source code on github | ||
</a> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
res.send(html) | ||
}) | ||
|
||
router.get('/users', (req, res) => { | ||
res.json({ | ||
users: [ | ||
{ | ||
name: 'steve' | ||
}, | ||
{ | ||
name: 'joe' | ||
} | ||
] | ||
}) | ||
}) | ||
|
||
router.get('/hello/', function(req, res) { | ||
res.send('hello world') | ||
}) | ||
|
||
// Attach logger | ||
app.use(morgan(customLogger)) | ||
|
||
// Setup routes | ||
app.use(routerBasePath, router) | ||
|
||
// Apply express middlewares | ||
router.use(cors()) | ||
router.use(bodyParser.json()) | ||
router.use(bodyParser.urlencoded({ extended: true })) | ||
|
||
return app | ||
} | ||
|
||
function customLogger(tokens, req, res) { | ||
const log = [ | ||
tokens.method(req, res), | ||
tokens.url(req, res), | ||
tokens.status(req, res), | ||
tokens.res(req, res, 'content-length'), | ||
'-', | ||
tokens['response-time'](req, res), | ||
'ms' | ||
].join(' ') | ||
|
||
if (process.env.NODE_ENV !== 'dev') { | ||
// Log only in AWS context to get back function logs | ||
console.log(log) | ||
} | ||
return log | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
exports.metadata = { | ||
name: 'Authenticated Fetch: uses node-fetch and Netlify Identity to access APIs', | ||
value: 'auth-fetch', | ||
short: 'auth-fetch' | ||
} | ||
exports.onComplete = () => { | ||
console.log(`authenticated node-fetch function created from template!`) | ||
console.log('REMINDER: Make sure to install `node-fetch` if you dont have it.') | ||
console.log( | ||
'REMINDER: Make sure to call this function with the Netlify Identity JWT. See https://netlify-gotrue-in-react.netlify.com/ for demo' | ||
) | ||
} | ||
|
||
exports.templateCode = () => { | ||
return ` | ||
// for a full working demo of Netlify Identity + Functions, see https://netlify-gotrue-in-react.netlify.com/ | ||
const fetch = require('node-fetch') | ||
exports.handler = async function(event, context) { | ||
if (!context.clientContext && !context.clientContext.identity) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ | ||
msg: | ||
'No identity instance detected. Did you enable it?' | ||
}) // Could be a custom message or object i.e. JSON.stringify(err) | ||
}; | ||
} | ||
const { identity, user } = context.clientContext; | ||
try { | ||
const response = await fetch('https://api.chucknorris.io/jokes/random'); | ||
if (!response.ok) { | ||
// NOT res.status >= 200 && res.status < 300 | ||
return { statusCode: response.status, body: response.statusText }; | ||
} | ||
const data = await response.json(); | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ identity, user, msg: data.value }) | ||
}; | ||
} catch (err) { | ||
console.log(err); // output to netlify function log | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err) | ||
}; | ||
} | ||
} | ||
` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
exports.metadata = { | ||
name: 'Serverless HTTP: dynamic serverside rendering via functions', | ||
value: 'serverless-http', | ||
short: 'serverless-http' | ||
} | ||
exports.onComplete = () => { | ||
console.log(`serverless-http function created from template!`) | ||
console.log('REMINDER: Make sure to `npm install serverless-http express cors morgan body-parser compression`.') | ||
} | ||
exports.copyAssets = ['app/index.js'] | ||
|
||
exports.templateCode = () => { | ||
return ` | ||
// for a full working demo check https://express-via-functions.netlify.com/.netlify/functions/serverless-http | ||
const serverless = require('serverless-http') | ||
const expressApp = require('./app') | ||
// We need to define our function name for express routes to set the correct base path | ||
const functionName = 'serverless-http' | ||
// Initialize express app | ||
const app = expressApp(functionName) | ||
// Export lambda handler | ||
exports.handler = serverless(app) | ||
` | ||
} |