A simple blog powered by a frontend single page application with backend serverless DynamoDB API
- JavaScript - By using JavaScript, we're unifying our development around one language for both backend and frontend
- JSX - This is a specific JavaScript file that allows HTML tags (such as React components) in a JavaScript file
- TypeScript/TSX - Our files are written in this. It's a JavaScript superset that adds static typing and strict syntactial support to our application. It's transcompiled into vanilla JavaScript behind the scenes.
- NodeJS - Node is JavaScript code that's stripped of all HTML and browser references that executes outside of a browser
- npm - Node Package Manager interacts with the npm repository and installs, upgrades, and removes NodeJS packages found
in the
package.json
file. It can aslo run any script commands listed in thescripts
tag ofpackage.json
such asnpm run offline
- Serverless (Concept): Traditionally, we deploy apis and websites to always running servers, even when no one is accessing the hosted content. With serverless computing platforms such as AWS Lambda, the cloud provider provisions resources when your code is requested and then deallocates it when no longer needed. It's also scalable for the same reason. However, if the resource hasn't been access in some time, the next request will take a bit longer than normal (cold start) though it's possible to solve that problem via serverless framework plugins
- Serverless Framework (npm package): This allows us to create simplified AWS CloudFormation templates for serverless function handlers and deploy them to AWS. Additionally, with plugins, we can run our code in an offline development environment and create small optimized builds with tree shaking
- Fully tested api using mocha
- Fully tested app using jest
- Hybrid local/online environment
- Shareable/Configurable (maybe make it an npm package)
It's the 21st century. It's easy to make a blog with content management systems like WordPress, Wix, and SquareSpace. They're great approaches and do the job well. They may even be a better option to get started quick so you can post content right away. I chose this approach for the following reasons:
- I really like serverless development
- I like the flexibility of using React components to create whatever I want without needing to learn how to plug it in to one of the aforementioned content management systems
- It's environmentally friendly since code runs on demand instead of being constantly provisioned
- It's cheaper since code runs on demand
- It's easy to test locally
- I wanted an excuse to program something cause it's really stimulating
- There's a good amount of documentation from the JavaScript community and AWS with examples of serverless development
This assumes you're using Visual Studio Code to setup your local development environment and have the following installed:
From a terminal or shell, enter the following after changing to a directory of your choice
git clone https://github.com/lahiyam/simple-blog.git
Create a new VSC workspace or open existing one.
Then, open the folder that was just created by git
From Visual Studio Code Settings
, expand the Extensions
tab and select Npm
Make sure Enable Run From Folder
and Enable Script Explorer
are checked. This will allow you to right click a folder
that contains a package.json
file and instruct VSC to run a script that you choose. You'll also see a new tool window
called NPM SCRIPTS
where you can run scripts found in any package.json
files
The api
and app
are in separate directories. You'll need to install dependencies for each directory.
Right click the api
folder, select Run NPM Script in Folder...
and select install
in the following context menu.
Once the terminal completes installation, repeat the same for the app
directory
Right click the api
folder, select Run NPM Script in Folder...
and select offline
to launch an API Gateway server
on http://localhost:3000/
Once Serverless shows a message in the terminal that it's listening on http://localhost:3000/
, open
http://localhost:3000/hello to make sure everything worked as expected
Any changes you make to TypeScript (.ts)
files in the api
directory will cause the application to automatically
(and quickly) rebuild so you can test changes immediately.
Right click the app
folder, select Run NPM Script in Folder...
and select start
to launch a local development
build. When prompted, allow React to run on a different port, enter Y
and press enter.
Once it's ready, a new browser window should open to http://localhost:3001/ automatically
Any changes you make to the app
directory will cause the application to automatically
(and quickly) rebuild as well as refresh the browser so you can test changes immediately. (Hot Reloading)
- JavaScript
- React - Single Page Application framework developed by Facebook
- Reactstrap - prebuilt JSX components
- Bootstrap - for CSS class name helpers that can be applied to components