angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.13k stars 1.24k forks source link

shared.js exports incorrectly and no longer copies process.env variables #2710

Open rohanrichards opened 6 years ago

rohanrichards commented 6 years ago
Item Version
generator-angular-fullstack 5.0.0-beta.3
Node 9.8.0
npm 5.6.0
Operating System Windows 10
Item Answer
Transpiler TypeScript
Markup HTML
CSS SCSS
Router ?
Client Tests Mocha
DB MongoDB
Auth Y

When accessing constants like so import constants from './app.constants'; The object is defined with this structure:

defaults: {
  env:
  etc"
}

Within app.module.ts an if statement appears which ads an injectable to the app (I don't understand what it does)

if (constants.env === 'development') {
    @Injectable()
    class HttpOptions extends BaseRequestOptions {
        merge(options: RequestOptionsArgs): RequestOptions {
            options.url = `http://localhost:9000${options.url}`;
            return super.merge(options);
        }
    }

    providers.push({ provide: RequestOptions, useClass: HttpOptions });
}

The if statement always fails as constants.env is always undefined. The correct way to access the variable would be with constants.default.env

Within server/config/environment/shared.js the export appears as module.exports.default and I have changed this to remove the default. Now my constants object can be accessed as above (constants.env)

This works fine, however I now have a CORS error when I run my app: CORS header ‘Access-Control-Allow-Origin’ missing I believe this is due to the Injectable code within app.module.ts (commenting out the code resolves the issue).

Finally, while testing shared.js I have noticed that it does not correctly import any process.env variables, and it seems the gulpfile is missing the task to copy them across. What is the appropriate way currently to get environment variables to the client side?

koraysels commented 5 years ago

I now do this:

export const env = process.env.NODE_ENV;
export const port = process.env.PORT || 9000;
// List of user roles
export const userRoles = ['guest', 'user', 'admin'];

export default {
    env,
    port,
    userRoles,
};

that seems to work...