├── scripts ├── object-type.json ├── objects.json └── import.js ├── views ├── pages │ ├── default.ejs │ └── 404.ejs └── partials │ ├── footer.ejs │ ├── nav.ejs │ └── header.ejs ├── serverless.yml ├── package.json ├── app.js ├── .gitignore └── README.md /scripts/object-type.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "Pages", 3 | "slug": "pages", 4 | "singular": "page" 5 | } -------------------------------------------------------------------------------- /views/pages/default.ejs: -------------------------------------------------------------------------------- 1 | <%- include('../partials/header') %> 2 |
3 |

<%= page.title %>

4 |
<%- page.content %>
5 | <%- include('../partials/nav') %> 6 |
7 | <%- include('../partials/footer') %> 8 | -------------------------------------------------------------------------------- /views/partials/footer.ejs: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /views/partials/nav.ejs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /views/pages/404.ejs: -------------------------------------------------------------------------------- 1 | <%- include('../partials/header') %> 2 |
3 |

404 Page Not Found

4 |
Oops! The content you are looking for does not exist in your Cosmic JS Bucket.
5 | <%- include('../partials/nav') %> 6 |
7 | <%- include('../partials/footer') %> -------------------------------------------------------------------------------- /scripts/objects.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "title": "Home", 4 | "slug": "home", 5 | "content": "This is the home page content.", 6 | "type_slug": "pages" 7 | }, 8 | { 9 | "title": "About", 10 | "slug": "about", 11 | "content": "This is the about page content.", 12 | "type_slug": "pages" 13 | }, 14 | { 15 | "title": "Contact", 16 | "slug": "contact", 17 | "content": "This is the contact page content.", 18 | "type_slug": "pages" 19 | } 20 | ] -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | # serverless.yml 2 | 3 | service: cosmic-serverless 4 | 5 | provider: 6 | name: aws 7 | runtime: nodejs6.10 8 | stage: dev 9 | region: us-east-1 10 | environment: 11 | COSMIC_BUCKET: ${env:COSMIC_BUCKET} 12 | COSMIC_READ_KEY: ${env:COSMIC_READ_KEY} 13 | 14 | functions: 15 | app: 16 | handler: app.handler 17 | events: 18 | - http: ANY / 19 | - http: 'ANY {proxy+}' 20 | plugins: 21 | - serverless-offline -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-starter", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "dependencies": { 7 | "cosmicjs": "^3.2.7", 8 | "ejs": "^2.6.1", 9 | "express": "^4.16.3", 10 | "nodemon": "^1.17.4", 11 | "opn": "^5.3.0", 12 | "serverless-http": "^1.6.0", 13 | "cryptiles": ">=4.1.2" 14 | }, 15 | "devDependencies": { 16 | "serverless-offline": "^3.25.5" 17 | }, 18 | "scripts": { 19 | "dev": "STAGE=local sls offline start", 20 | "develop": "npm run dev", 21 | "import": "node ./scripts/import.js" 22 | }, 23 | "author": "", 24 | "license": "ISC" 25 | } 26 | -------------------------------------------------------------------------------- /views/partials/header.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= page.title %> 5 | 6 | 35 | 36 |

Cosmic JS Serverless Starter

-------------------------------------------------------------------------------- /scripts/import.js: -------------------------------------------------------------------------------- 1 | const Cosmic = require('cosmicjs') 2 | const api = Cosmic() 3 | const COSMIC_BUCKET = process.env.COSMIC_BUCKET || 'node-starter' 4 | const bucket = api.bucket({ 5 | slug: COSMIC_BUCKET, 6 | read_key: process.env.COSMIC_READ_KEY, 7 | write_key: process.env.COSMIC_WRITE_KEY 8 | }) 9 | const default_objects = require('./objects') 10 | const default_object_type = require('./object-type') 11 | const importObjects = async () => { 12 | try { 13 | const res = await bucket.getObjects() 14 | if(res.status === 'empty') { 15 | bucket.addObjectType(default_object_type) 16 | default_objects.forEach(object => { 17 | bucket.addObject(object) 18 | }) 19 | } 20 | } catch (e) { 21 | console.log(e) 22 | } 23 | } 24 | importObjects() 25 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const serverless = require('serverless-http') 2 | const express = require('express') 3 | const app = express() 4 | const PORT = process.env.PORT || 3000 5 | const Cosmic = require('cosmicjs') 6 | const api = Cosmic() 7 | const COSMIC_BUCKET = process.env.COSMIC_BUCKET || 'node-starter' 8 | const COSMIC_READ_KEY = process.env.COSMIC_READ_KEY || '' 9 | let stage_path = 'dev/' 10 | if (process.env.STAGE === 'local') 11 | stage_path = '' 12 | const bucket = api.bucket({ 13 | slug: COSMIC_BUCKET, 14 | read_key: COSMIC_READ_KEY 15 | }) 16 | app.set('view engine', 'ejs') 17 | app.get('/:slug?', (req, res) => { 18 | let slug = req.params.slug 19 | const year = (new Date().getFullYear()) 20 | if (!slug) 21 | slug = 'home' 22 | bucket.getObject({ slug }).then(data => { 23 | const page = data.object 24 | res.render('pages/default', { page, year, stage_path }) 25 | }).catch(err => { 26 | const page = { title: 'Page not found' } 27 | res.render('pages/404', { page, year, stage_path }) 28 | }) 29 | }) 30 | 31 | module.exports.handler = serverless(app); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | .DS_Store 64 | 65 | # serverless folder 66 | .serverless -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Starter 2 | Serverless Node.js starter app powered by [Cosmic JS](https://cosmicjs.com) 🚀 3 | 4 | ## Installation 5 | Install via the [Cosmic CLI](https://github.com/cosmicjs/cosmic-cli). 6 | ```bash 7 | npm i -g cosmic-cli 8 | 9 | # Login to your Cosmic JS account 10 | cosmic login 11 | 12 | # Installs example content to a new or existing Bucket and downloads the app locally 13 | cosmic init serverless-starter 14 | cd serverless-starter 15 | ``` 16 | ## Install Serverless 17 | Install [Serverless](https://serverless.com) and login. 18 | ``` 19 | npm i -g serverless 20 | serverless login 21 | ``` 22 | ## Run locally 23 | Start serverless server locally. 24 | ```bash 25 | # starts app locally connected to your Cosmic JS Bucket 26 | cosmic develop 27 | ``` 28 | ## Configure AWS credentials 29 | Add your AWS keys. Follow the [guide on the Serverless website](https://serverless.com/framework/docs/providers/aws/guide/credentials/) for more instructions. 30 | ``` 31 | export AWS_ACCESS_KEY_ID= 32 | export AWS_SECRET_ACCESS_KEY= 33 | ``` 34 | ## Deploy to AWS 35 | Deploying to AWS is easy using the Serverless deploy command. 36 | ``` 37 | COSMIC_BUCKET=your-bucket-slug sls deploy 38 | ``` 39 | ## [Serverless CMS](https://cosmicjs.com/knowledge-base/serverless-cms) 40 | Cosmic JS offers a [Headless CMS](https://cosmicjs.com/headless-cms) for your Serverless websites and apps. 41 | --------------------------------------------------------------------------------