# Deployment
Deploying your ExpressWebJs application is very easy. You need a server that can install and run Node.js >= 14.15.4.
# Compiling from TypeScript to JavaScript
ExpressWebJs version 3 applications are written in TypeScript and must be compiled to JavaScript during deployment. You can archive this by compiling your application directly on the production server or perform the build step in a CI/CD pipeline.
You can build your application for production by running the build command.
npm run build
The compiled JavaScript output is moved to the build directory.
Once that is done, you need to change your application environment to production in .env file
APP_ENV = production;
# Installing Nodejs and NPM in Ubuntu
Node.js can be installed in multiple ways on your Ubuntu Linux machine. You can use Ubuntu’s official repository to install Node.js or another way is to use NodeSource repository. Installation via NodeSource repository will allow you to choose latest version of Node.js.
Install Node.js using Ubuntu official repository: Node.js is available in Ubuntu’s repository and you can easily install it using a few commands. Follow the steps below to install Node.js on your Ubuntu operating system.
Step 1: To install node.js use the following command:
sudo apt install nodejs
Step 2: Once installed, verify it by checking the installed version using the following command:
node -v or node –version
Note: It is recommended to install Node Package Manager(NPM) with Node.js. NPM is an open source library of Node.js packages. To install NPM, use the following commands:
sudo apt install npm
To check NPM version, use the following commands:
npm -v or npm –version
Node and NPM will be successfully installed on your Ubuntu machine.
Install Node.js using NodeSouce repository: The latest version of Node.js can be installed from NodeSource repository (opens new window). Follow the steps below to install the Node.js on your Ubuntu.
Step 1: Use the following commands to update and upgrade the package manager:
sudo apt-get update
sudo apt-get upgrade
Step 2: Install Python software libraries using the following command:
sudo apt-get install python-software-properties
Step 3: Add Node.js PPA to the system:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash –
Note: Here, we are installing node.js version 14, if you want to install version 13, you can replace setup_14.x with setup_13.x.
Step 4: To Install Node.js and NPM to your Ubuntu machine, use the command given below:
sudo apt-get install nodejs
Step 5: Once installed, verify it by checking the installed version using the following command:
node -v or node –version
npm -v or npm –version
Finally, you have successfully installed Node.js and NPM on your Ubuntu machine.
# Using a process manager
In development, you started your app simply from the command line with node app.ts file. But doing this in production is not cool. If the app crashes, it will be offline until you restart it. To ensure your app restarts if it crashes, you need to use a process manager. A process manager is a “container” for applications that facilitates deployment, provides high availability, and enables you to manage the application at runtime.
In addition to restarting your app when it crashes, a process manager can enable you to:
- Gain insights into runtime performance and resource consumption.
- Modify settings dynamically to improve performance.
- Control clustering (StrongLoop PM and pm2).
The most popular process managers for Node are as follows:
- StrongLoop Process Manager
- PM2
- Forever
For a feature-by-feature comparison of the three process managers, see strong-pm.io/compare (opens new window).
Here is a PM2 sample config file:
{
"name": "WEB_APP",
"script": "npm run start",
"watch": true,
"ignore_watch": ["Storage"],
"watch_options": {
"followSymlinks": false
}
}
In your project root, you can create pm2-process.json file to house your pm2 configuration.
Once that is done, we can now run the below code to start your application:
pm2 start pm2-process.json
# Logging
ExpressWebJs logger write logs to stdout and stderr. You can forward them to a local file on the same server or set up an external logging service to read the logs from stdout/stderr.
Below is a sample code showing how to log some activities or events in your application
import Log from "Elucidate/Log";
class UserService {
public async getAllUsers() {
try {
// Code to query users
} catch (error) {
Log.error("App/Service/UserService", "Error Occured while fetching user", error);
return error;
}
}
}