MEAN Stack (MongoDB, Express, Angular, Node.js)

repository·master·Indexed 12 days ago

https://github.com/linnovate/mean

A starting point for building cloud-native, fullstack JavaScript applications using MongoDB, Express, Angular, and Node.js. Version 2.0.2 includes a pre-configured Express server, Angular frontend with authentication services, HTTP interceptors for error handling, and Docker Compose support for deployment.

Tokens
3.3K
Snippets
16
Records
18
Agent score
96%

What's inside MEAN

  1. What is the MEAN stack?

    master

    The MEAN stack is an end-to-end framework for building cloud-native fullstack JavaScript applications. It consists of four primary open-source components:

    • MongoDB: A document database used by the back-end to store data as JSON documents.
    • Express (Express.js): A back-end web application framework running on Node.js.
    • Angular: A front-end web application framework that runs in the browser to provide a dynamic UI.
    • Node.js: A JavaScript runtime environment used to implement the application back-end.
  2. Install and run the MEAN stack locally

    master

    To set up the project for local development using yarn, clone the repository, configure your environment variables from the example file, install dependencies, and start the development server.

    git clone https://github.com/linnovate/mean
    cd mean
    cp .env.example .env
    yarn
    yarn start
  3. Run the MEAN stack using Docker

    master

    You can run the stack using Docker Compose.

    Requirement: Ensure your Docker version is 19.03.0+.

    git clone https://github.com/linnovate/mean
    cd mean
    cp .env.example .env
    docker-compose up -d
  4. Initialize and start the Express server

    master

    The server/index.js file serves as the entrypoint for the application. It orchestrates the loading of configuration, the Express application instance, and the Mongoose database connection.

    Important: The config module must be imported before any other module to ensure environment variables and settings are correctly applied across the application.

    When run directly (not required as a module), the server starts listening on the port specified in config.port.

    // To start the server directly via node:
    node server/index.js
    
    // Or if importing into another script:
    const app = require('./server/index');
  5. Bootstrap the Angular application

    master

    The application is bootstrapped using platformBrowserDynamic() which initializes the Angular platform in the browser. It loads the root module, AppModule, to start the application lifecycle. If the environment.production flag is set to true, enableProdMode() is called to enable production optimizations (such as disabling double-check change detection).

    import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    
    if (environment.production) {
      enableProdMode();
    }
    
    platformBrowserDynamic()
      .bootstrapModule(AppModule)
      .catch(err => console.log(err));
  6. Configure the mean application via Docker Compose

    master

    The mean stack can be deployed using Docker Compose. The setup includes an app service (the application) and a mongo service (the database).

    Application Service (app)

    • Ports: Maps host port 4040 to container port 4040.
    • Restart Policy: Set to always.
    • Dependencies: Depends on the mongo service being started.

    Environment Variables

    The app service uses the following environment variables for configuration:

    • NODE_ENV: Set to production by default.
    • SERVER_PORT: The port the application listens on (default 4040).
    • JWT_SECRET: The secret key used for JSON Web Token signing.
    • MONGO_HOST: The connection string for MongoDB (default mongodb://mongo/mean).

    Database Service (mongo)

    • Image: Uses mongo:4.2.
    • Persistence: Uses a named volume mongo_data mapped to /data/db to ensure data persists across container restarts.
    version: '3.8'
    
    services:
      app:
        build: ./
        image: mean
        ports:
          - 4040:4040
        environment:
          NODE_ENV: production
          SERVER_PORT: 4040
          JWT_SECRET: 0a6b944d-d2fb-46fc-a85e-0295c986cd9f
          MONGO_HOST: mongodb://mongo/mean
        restart: always
        depends_on:
          - mongo
    
      mongo:
        image: mongo:4.2
        volumes:
          - mongo_data:/data/db
    
    volumes:
      mongo_data:
  7. Use the exported routing guards

    master

    The src/app/shared/guards/index.ts file serves as the public entry point for all routing guards in the application. It re-exports the contents of ./auth.guard. Use this entry point to import guards for use in your application's routing configuration.

    import { AuthGuard } from './shared/guards';
  8. Use CatchErrorInterceptor to handle API errors with snackbars

    master

    The CatchErrorInterceptor is an Angular HttpInterceptor designed to automatically catch HttpErrorResponse objects from outgoing HTTP requests. When an error occurs, it extracts an error message from the response (prioritizing response.error.message, falling back to response.error.statusText) and displays it to the user via a MatSnackBar (using MatLegacySnackBar) for 2000ms. After showing the snackbar, it re-throws the error using throwError so that the calling service can still handle the error logic if needed.

    // To use this interceptor, you must provide it in your AppModule or ApplicationConfig
    // within the HTTP_INTERCEPTORS multi-provider array.
    
    import { HTTP_INTERCEPTORS } from '@angular/common/http';
    import { CatchErrorInterceptor } from './path/to/http-error.interceptor';
    
    // In your providers array:
    // { provide: HTTP_INTERCEPTORS, useClass: CatchErrorInterceptor, multi: true }
  9. Export the Express application instance

    master

    The server/index.js file exports the initialized Express app instance. This allows the application to be imported into other modules, such as testing frameworks (e.g., Mocha) or integration scripts, without automatically starting the network listener.

    const app = require('./server/index');
    // 'app' is the Express application instance configured in ./config/express