Skip to content

Code structure and boilerplate that could help you out at organizing your cool API project using Express

License

Notifications You must be signed in to change notification settings

Camilla9347/express-starterkit

 
 

Repository files navigation

express-starterkit

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.

Folder structure

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.

What's included?

Development

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

Start the server with

npm start

Relative requires

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.

Installing additional dependencies

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

Why npm run i and npm run u?

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.

TODO

  • Add tests examples
  • Add Swagger
  • Add celebrate
  • Add PostgreSQL example

About

Code structure and boilerplate that could help you out at organizing your cool API project using Express

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%