├── .env.example ├── .gitignore ├── LICENSE ├── README.md ├── cli.js ├── handler.js ├── lib ├── hello.js └── response.js ├── package-lock.json ├── package.json ├── serverless.yml ├── test-data.json └── test └── hello.test.js /.env.example: -------------------------------------------------------------------------------- 1 | STAGE=develop -------------------------------------------------------------------------------- /.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 | .env.develop 60 | .env.staging 61 | .env.production 62 | # next.js build output 63 | .next 64 | 65 | # serverless 66 | .serverless -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Eslam Salem 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-nodejs-starter-template 2 | A boilerplate for new Serverless Nodejs projects with tests and basic configurations 3 | 4 | 5 | ## Description 6 | Simple hello function take name and return with hello 7 | 8 | 9 | ``` javascript 10 | const hello = require('./lib/hello'); 11 | const response = require('./lib/response'); 12 | 13 | const helloFn = async (event) => { 14 | 15 | let body = event.body ? JSON.parse(event.body) : event; //api gateway (event.body) vs invoke (event) 16 | 17 | let result = hello(body); 18 | 19 | return response(result); 20 | } 21 | 22 | module.exports.hello = helloFn 23 | ``` 24 | 25 | ## features 26 | * Ready to use default configuration for serverless framework 27 | * Environment variables ready with develop, staging and production 28 | * Works with both invoke and api 29 | * Serverless offline to test the api gateway localy 30 | 31 | ## Installation 32 | 33 | * Install & Configure serverless framework, Guide [here](https://serverless.com/framework/docs/getting-started/) 34 | 35 | * Clone this repo and run `npm install` then `npm run config` 36 | 37 | 38 | ## Test locally 39 | 40 | * `npm run local` will run the function directly without serverless framework for fast test on your local machine 41 | * `npm run invoke` will invoke the function locally and pass the data in `test-data.json` to your function as body 42 | * `npm run api` will run api gateway locally in your machine on port `3000` so you can test the function as it on AWS api gateway 43 | 44 | ## Deploy 45 | * `npm run deploy` will deploy on production and load your .env.production environment variables 46 | * `npm run deploy-develop` will deploy on develop and load your .env.production environment variables 47 | * `npm run deploy-staging` will deploy on staging and load your .env.production environment variables 48 | 49 | 50 | 51 | ## Contribution 52 | 53 | Feel free to fork, commit and submit pull request if you find a bug, or you want to add support to a new environment. Contributions are very welcome. 54 | 55 | ## License 56 | 57 | MIT 58 | 59 | 60 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | 3 | const handler = require('./handler'); 4 | 5 | ( async() => { 6 | 7 | let result = await handler.hello({ 8 | body: { 9 | name: "cli" 10 | } 11 | }); 12 | console.log(result); 13 | } ) () 14 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | 2 | const hello = require('./lib/hello'); 3 | const response = require('./lib/response'); 4 | 5 | const helloFn = async (event) => { 6 | 7 | let body = event.body ? JSON.parse(event.body) : event; //api gateway (event.body) vs invoke (event) 8 | 9 | let result = hello(body); 10 | 11 | return response(result); 12 | } 13 | 14 | module.exports.hello = helloFn 15 | -------------------------------------------------------------------------------- /lib/hello.js: -------------------------------------------------------------------------------- 1 | module.exports = (body) => { 2 | let name = body.name || 'unknown'; 3 | return "Hello " + name + " :)"; 4 | } -------------------------------------------------------------------------------- /lib/response.js: -------------------------------------------------------------------------------- 1 | module.exports = (message) => { 2 | 3 | //let body = message; //default; 4 | 5 | let body = JSON.stringify( message , null, 2 ) 6 | 7 | return { 8 | statusCode: 200, 9 | body 10 | } 11 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-nodejs-starter-template", 3 | "version": "1.0.0", 4 | "description": "A boilerplate for new Serverless Nodejs projects with tests and basic configurations", 5 | "main": "cli.js", 6 | "directories": { 7 | "lib": "lib", 8 | "test": "test" 9 | }, 10 | "dependencies": {}, 11 | "devDependencies": { 12 | "dotenv": "^8.2.0", 13 | "jest": "^24.9.0", 14 | "serverless-dotenv-plugin": "^2.1.1", 15 | "serverless-offline": "^5.12.0" 16 | }, 17 | "scripts": { 18 | "config": "cp .env.example .env && cp .env.example .env.develop && cp .env.example .env.staging && cp .env.example .env.production", 19 | "deploy": "NODE_ENV=production sls deploy", 20 | "deploy-staging": "NODE_ENV=staging sls deploy", 21 | "deploy-develop": "NODE_ENV=develop sls deploy", 22 | "local": "node cli.js", 23 | "invoke": "sls invoke local -f hello -p test-data.json", 24 | "api": "sls offline start", 25 | "test": "jest -i" 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/netcode/serverless-nodejs-starter-template.git" 30 | }, 31 | "author": "", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/netcode/serverless-nodejs-starter-template/issues" 35 | }, 36 | "homepage": "https://github.com/netcode/serverless-nodejs-starter-template#readme" 37 | } 38 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | # Welcome to Serverless! 2 | # 3 | # This file is the main config file for your service. 4 | # It's very minimal at this point and uses default values. 5 | # You can always add more config options for more control. 6 | # We've included some commented out config examples here. 7 | # Just uncomment any of them to get that config option. 8 | # 9 | # For full config options, check the docs: 10 | # docs.serverless.com 11 | # 12 | # Happy Coding! 13 | 14 | service: my-service 15 | 16 | provider: 17 | name: aws 18 | runtime: nodejs10.x 19 | 20 | # you can overwrite defaults here 21 | stage: ${env:STAGE} 22 | # region: us-east-1 23 | 24 | 25 | functions: 26 | hello: 27 | handler: handler.hello 28 | events: 29 | - http: 30 | path: hello 31 | method: post 32 | 33 | plugins: 34 | - serverless-dotenv-plugin 35 | - serverless-offline -------------------------------------------------------------------------------- /test-data.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Invoke" 3 | } -------------------------------------------------------------------------------- /test/hello.test.js: -------------------------------------------------------------------------------- 1 | const hello = require('../lib/hello'); 2 | 3 | test('hello test', () => { 4 | let res = hello({name: "test"}); 5 | expect(res).toBe("Hello test :)"); 6 | }); --------------------------------------------------------------------------------