Git Product home page Git Product logo

express-starterkit's Introduction

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

Create the environment variables by issuing the following

cp .env.example .env

Then 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.

Heroku

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.

TODO

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

express-starterkit's People

Contributors

dependabot[bot] avatar jorgeramirez avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.