How to deploy a nx workspace (angular + nestjs) easily with ZEIT Now?
Tags: nx, angular, nestjs, now
Published: 2019-12-21 11:46:37

##What is the purpose of this article?
In this article we will create a nx workspace with angular webapp and nestjs api. I will provide a easy way to deploy this full stack project with ZEIT Now.

##What is nx?
Nx is a set of extensible dev tools for monorepos, which helps you develop like Google, Facebook, and Microsoft.
It has first-class support for many frontend and backend technologies. So we will use nx to create a full stack application in the next sections.

##What is ZEIT Now?
Zeit Now is a cloud platform for serverless deployment. It's an incredibly simple, easy to use platform that allows you to deploy anything from static websites to server/serverless application instantly, scale automatically, all with minimal configuration.
This includes front-end applications (Angular/React/Vue/etc), or any backend of your choosing - Go, Node.js, Python and everything in-between!
NestJS is a Node.js framework after all, so how can we take advantage of Now, and deploy our applications ?

##Create a full stack nx workspace
Open a terminal and run this command to create a new workspace :
<pre>
npx create-nx-workspace@latest
</pre>

Wen asked provide the same answers as following :
<pre>
C:\Users\khali\devto>npx create-nx-workspace@latest
npx: installed 167 in 17.821s
? Workspace name (e.g., org name)     myorg
? What to create in the new workspace angular-nest      [a workspace with a full stack application (Angular + Nest)]
? Application name                    myapp
? Default stylesheet format           SASS(.scss)  [ http://sass-lang.com   ]
Creating a sandbox with Nx...
</pre>

##Serve the newly created application
In the same terminal run this commands to serve the api : 
<pre>
cd myorg
ng serve api
</pre>
Open another terminal and do the same for the angular app : 
<pre>
cd myorg
ng serve myapp
</pre>
open http://localhost:4200 to see both the frontend and backend working.


##Setting up Zeit Now
###Install the Now CLI
Since everything work good locally, we will deploy now our application using Zeit Now. So exit the the two terminal and run the following command to install Zeit Now globally.
<pre>
npm i -g now
</pre>

Make sure you're logged into the Now CLI (or create an account before logging in).
Type in the terminal :
<pre>
now login
</pre>
You will be asked to enter your email.
Follow the instructions until you get the confirmation :

<pre>
C:\Users\khali\devto\myorg>now login
> We sent an email to me@example.com. Please follow the steps provided
  inside it and make sure the security code matches Delightful Zonkey.
√ Email confirmed
> Congratulations! You are now logged in. In order to deploy something, run `now`.

C:\Users\khali\devto\myorg>
</pre>

###Configuring Zeit Now for Our application
Typically with Now, deployments are as simple as typing now in your terminal.

At the root of your application, create a now.json file and add the JSON code below.
<pre>
{
    "version": 2,
    "name": "myapp",
    "builds": [
      { "src": "/dist/apps/api/main.js", "use": "@now/node" },
      { "src": "/dist/apps/myapp/*", "use": "@now/static" }
    ],
    "routes": [
      { "src": "/api/(.*)", "dest": "/dist/apps/api/main.js" },
      { "handle": "filesystem" },
      { "src": "/assets/(.*)", "dest": "/dist/apps/myapp/assets/$1"},
      { "src": "/(.*).(js|css|ico)", "dest": "/dist/apps/myapp/$1.$2" },
      { "src": "/(.*)", "dest": "/dist/apps/myapp/index.html" }
    ]
  }
</pre>

###Building & Deploying

Now that we have everything setup - let's Deploy it to Now!

NestJS is a TypeScript-based Node.js framework, so we're going to need to make sure we build it for Production (via ng build) and then we can let Now do its thing (via now) !!

<pre>
    ng build --prod myapp && ng build --prod api && now --prod
</pre>


We wait until Now finish doing the magic :

<pre>
C:\Users\khali\devto\myorg>ng build --prod myapp && ng build --prod api && now --prod
...
...
> Deploying ~\devto\myorg under khalillou
> Using project myapp
> Synced 1 file [843ms]
> https://myapp-pscusvfx7.now.sh [3s]
> Ready! Deployment complete [25s]
- https://myapp-ten-henna.now.sh
- https://myapp.khalillou.now.sh [in clipboard]

C:\Users\khali\devto\myorg>
</pre>

Open the link given by Now, and you get the same result as locally but now deployed on the cloud.

You can find the Github code example here :
https://github.com/khalilou88/nx-workspace-now