This repository contains a simple code structure and boilerplate that could help you out at organizing your cool API project using Express.
This starter kit is inspired by this awesome repository.
src
app.js # The main entry point that create an instance of our application.
server.js # Script to start our server.
create-symlink.js # Script that we use to avoid doing ../../ for relative requires.
routes # We define Express routes here.
config # Environment variables and configuration.
loaders # Split the startup process into tiny modules.
models # Put your database-related code here (models, queries, etc).
services # Business tier: all the business logic is here.
- config management using dotenv
- eslint + prettier
- Travis CI for continuous integration
Procfile
file for deployments on Heroku.- nodemon to automatically restart the server upon file changes.
- Jest for testing.
This starter kit uses the node
version 12.13.0 (LTS version). You can use nvm to install and use this version.
nvm install 12.13.0
nvm use 12.13.0
We defined this version of node in the .nvmrc
already, so you could do the following instead:
nvm install
nvm use
Run the following command to install the dependencies of this starter kit (do this only the first time).
npm install
Create the environment variables by issuing the following
cp .env.example .env
Then start the server with
npm start
The create-symlink.js
script runs as a postinstall hook. It allows us to use the following pattern for relative requires within our project.
const User = require('@app/models/user');
// the rest of your code
Basically, @app
is a symlink to the src
folder. It plays nicely with IDEs (e.g., intellisense works). We also included a preinstall
hook to stop npm from erasing your src folder.
If you want to install a new dependency (e.g, node-fetch
), do the following:
npm run i -- --save node-fetch
And to uninstall
npm run u -- node-fetch
The trick shown above that allows us to use relative requires comes with a catch. If we want to install a node library, say node-fetch
we do:
npm install --save node-fetch
Doing this will erase the content of your src folder!! Even though we already have the preinstall hook. That's why we rely on the install-wrapper.js
to run preinstall
and postinstall
hooks when we install/uninstall dependencies.
Configure the following environment variable in your Heroku application to make sure Heroku does not delete the src
folder during the build and deployment.
heroku config:set NPM_CONFIG_PRODUCTION=false NODE_MODULES_CACHE=false --app [your app name]
You could also configure the environment variables using Heroku's dashboard Settings -> Reveal Config Vars
.
- Add tests examples
- Add Swagger
- Add celebrate
- Add PostgreSQL example