LemonMart Server
January 23, 2024 ยท View on GitHub
Easy to learn and use TypeScript Express.js server with REST, GraphQL, and MongoDB using Minimal MEAN for Lemon Mart
Watch the video: Do More with Less: Full Stack TypeScript
Get the book: Lemon Mart Server is covered in my book Angular for Enterprise Applications. You can get it at AngularForEnterprise.com.
Setup
- Install Node.js v20
- Recommended Editor/IDE: Visual Studio Code
- For a magical development experience, download these VS Code Extensions:
- Configure my preferred extentions.json and settings.json files.
npm install- This will kick off a script, which will run
npm installon all child folders. - Run
npm run init:envto configure your environment variables in.envfiles
Manually Setup Environment Variables
Skip over this if you already ran the automated command
- Define a
.envfile at the project's root and set the MongoDB admin password. Do NOT commit this file.
MONGODB_ADMIN_PASS=your_password_goes_here
MONGODB_APPLICATION_DATABASE=app_db_name
MONGODB_APPLICATION_USER=app_user
MONGODB_APPLICATION_PASS=app_password
MONGO_URI=uri_to_mongodb
-
See more details about the MongoDB Docker container at duluca/minimal-mongo, which also contains instructions on how to set things up on AWS ECS.
In your server application, use the application information to connect to the database. Sample connection URI:
mongodb://app_user:app_password@localhost:27017/app_db_name?readPreference=primary -
Sample
.envfile. Note: In configuring theMONGO_URI, instead of localhost or an IP address, you must specifydatabase, which is the container's name as defined indocker-compose.ymlfile.
MONGODB_ADMIN_PASS=admin
MONGODB_APPLICATION_DATABASE=acme
MONGODB_APPLICATION_USER=john.smith
MONGODB_APPLICATION_PASS=g00fy
MONGO_URI=mongodb://john.smith:g00fy@database/acme
- You need a separate
.envfile under Server for development purposes. Note: We specify localhost, not the docker-compose name here.
MONGO_URI=mongodb://john.smith:g00fy@localhost:27017/acme
Run
- From the root directory, run
npm start- This will kick off
docker-compose up, which will build and configure your web app, server, and database. - Angular Web App: http://localhost:8080
- Server: http://localhost:3000
- Database: http://localhost:27017
- This will kick off
- Run
npm stopornpm cleanto stop or clean Docker's cache.
Development
- For development purposes, run each service individually
- Angular Web App:
cd web-appthennpm start-- which utilizesng serveand will give you live reload. To debug, use Angular DevTools - Server:
cd serverthennpm run watchor use the debugger within VS Code (debug configuration is already included) - Database:
npm start:databasefrom the root
- Angular Web App:
Architecture
- web-app: This folder contains the client-side Angular app, configured using Angular CLI along with its own individual Node.js server
- server: This folder contains the server-side Node.js app that can be used to serve REST APIs, and it is capable of connecting to MongoDB
- document-ts: The library to connect and query Mongo in an async, flexible and convenient manner
- duluca/minimal-mongo: A fully-featured Mongo image (with Auth and SSL) inherited from the official image.
Continuous Integration and Hosting
- CI is implemented on CircleCI
- Hosted on AWS ECS
- You'll need to publish your Docker containers to ECS individually
- Then update
docker-compose.aws.ymlto pull from the ECS repository - Run
npm run publish:awson the root folder to create the task definition - You'll need to create a new service and attach this task definition to it
- See the Step-by-Step AWS ECS Guide on how to create container repositories, and attaching a task definition to a service here.
- See the Configuring AWS ECS to have access to AWS EFS Guide to persist data using MongoDB here.
REST
The OpenAPI schema for the project is hosted on /api-docs using SwaggerUI.
GraphQL
Apollo Explorer is hosted on /graphql.