DEV Community

Titouan B
Titouan B

Posted on

⚛️ Local HTTPS for React app in Nx workspace

Hey, welcome back in this post series where we will see how to setup a full HTTPS development environment.

In this post, we will setup local HTTPS for React app in Nx workspace.

If you don't have generated your certificate with mkcert, I recommand you read the first post of this series. → link

What is Nx?

nx

Nx is a set of extensible dev tools for monorepos, which helps you manage your projects at any scale. It provides great integration with major framework such as Angular, React, Nestframework, Express, ionic, ...

💡 Nx use the angular-cli under the hood!

Setting up the project workspace

Creating a new empty workspace

$ npx create-nx-workspace
npx : 179 installé(s) en 7.547s
? Workspace name (e.g., org name)     myorg
? What to create in the new workspace empty             [an empty workspace with a layout tha
t works best for building apps]
? CLI to power the Nx workspace       Nx           [Recommended for all applications (React, 
Node, etc..)]
...
Enter fullscreen mode Exit fullscreen mode

🗒️ If you already have an Nx workspace, you can skip these steps.

Then, we will install the Reactjs schematics:

npm install -D @nrwl/react
Enter fullscreen mode Exit fullscreen mode

Now, we will generate a new Reactjs application called react-app (change the name with your app name).

nx generate @nrwl/react:application --name=react-app
Enter fullscreen mode Exit fullscreen mode

Choose your settings such as stylesheet format, routing, ...

Start serving your app with nx serve react-app 🎉

http

🗒️ Look at the Reactjs Nx plugin documentation to see more options → here

Setting up HTTPS

From the first post of this series, I will assume that you have generated your certificate at location myorg/dev-stack/certs/local-cert.pem & myorg/dev-stack/certs/local-key.pem. Don't hesitate to go back to the first post to use mkcert and generate your certificate.

In the workspace.json, search for your app name (here react-app). Under this, look for the serve object and options. We will add the ssl configuration under "browserTarget": "react-app:build",.

The serve object in the workspace.json should look like this:

...
    "serve": {
          "builder": "@nrwl/web:dev-server",
          "options": {
            "buildTarget": "react-app:build",
            "host": "dev.local",
            "ssl": true,
            "sslKey": "./dev-stack/certs/local-key.pem",
            "sslCert": "./dev-stack/certs/local-cert.pem"
          },
          "configurations": {
            "production": {
              "buildTarget": "react-app:build:production"
            }
          }
        },
...
Enter fullscreen mode Exit fullscreen mode

Now save and serve the app:

$ nx serve react-app

> nx run react-app:serve 
**
Web Development Server is listening at https://dev.local:4200/
**
Starting type checking service...
Using 14 workers with 2048MB memory limit
ℹ 「wds」: Project is running at https://dev.local:4200/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: 404s will fallback to //index.html
No type errors found
Version: typescript 4.0.3
Time: 4026ms
Hash: cc02edd4220bb47802d9
Built at: 2020-10-08 16:49:42
Entrypoint main [big] = runtime.js runtime.js.map vendor.js vendor.js.map main.js main.js.map
Entrypoint polyfills [big] = runtime.js runtime.js.map polyfills.js polyfills.js.map
chunk {main} main.js, main.js.map (main) 359 KiB ={runtime}= ={vendor}= [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 569 KiB ={runtime}= [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 0 bytes ={main}= ={polyfills}= ={vendor}= [entry] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 1.09 MiB ={main}= ={runtime}= [initial] [rendered] split chunk (cache group: vendor) (name: vendor)
ℹ 「wdm」: Compiled successfully.

Enter fullscreen mode Exit fullscreen mode

You can open https://dev.local:4200 which is secured with a valid certificate 🔐🎉

https

certificate

Feel free to change any configuration on the options, but don't forget to regenerate a new certificate with mkcert of you change the domain name ⚠️

See you in the next post!

Github repository

GitHub logo Nightbr / full-https-development-environment

A full development environment in HTTPS with a valid certificate for your local development domain with mkcert, Nx workspace, angular, reactjs, nestjs, express, docker, traefik.

Myorg

This project was generated using Nx.

🔎 Nx is a set of Extensible Dev Tools for Monorepos.

Adding capabilities to your workspace

Nx supports many plugins which add capabilities for developing different types of applications and different tools.

These capabilities include generating applications, libraries, etc as well as the devtools to test, and build projects as well.

Below are our core plugins:

  • React
    • npm install --save-dev @nrwl/react
  • Web (no framework frontends)
    • npm install --save-dev @nrwl/web
  • Angular
    • npm install --save-dev @nrwl/angular
  • Nest
    • npm install --save-dev @nrwl/nest
  • Express
    • npm install --save-dev @nrwl/express
  • Node
    • npm install --save-dev @nrwl/node

There are also many community plugins you could add.

Generate an application

Run nx g @nrwl/react:app my-app to generate an application.

You can use any of the plugins above to generate applications as well.

When using Nx, you can create multiple applications and libraries in the same workspace.

Generate a library

Run nx

Latest comments (1)

Collapse
 
prakashgdev profile image
prakashg-dev

This is not working for me. May I know the reason please?

dev.to/prakashgdev/local-certifica...