Evolution of a Node.js API, Zoe.js — Getting Started
Workspace is ready so now we lay down the basics of the project:
- Web server
- Environment variables
- Nodemon
- package.json scripts
Web server
Organizing our source code is a fundamental step in every software project, it affects understandability and maintainability. Web server consists of an application and a server file.
Application
The source code and related configuration files live in the app
directory.
📦zoejs
┣ 📂.vscode
┃ ┗ 📜settings.json
┣ 📂app
┃ ┣ 📂config
┃ ┃ ┗ 📜app.js
┃ ┗ 📜server.js
┗ ...
Main application code placed in app/config/app.js
, this is where we bootstrap our app, creating Express.js
instance and set routes. As I mentioned Zoe.js
is built on Express.js
so let's install it.
npm install express
Now let’s see the application file.
Server
Web server is the starting point of everything. It runs on port 5000.
Start your engines
Head to the terminal and type:
node app/server.js
to start the server and our application. Now you can access it at http://localhost:5000
Environment variables
As you can see server port is hard coded to 5000. This is okay for now, but to have a flexible application we should make it configurable. The easiest solution to our problem is an environment variable.
An environment variable is a variable whose value is set outside the program.
Using an npm
package dotenv we have the option to load environment variables from a file called .env
which is in the project root folder. First install the dotenv
package:
npm install dotenv
Second create a new file .env
in the project root folder.
📦zoejs
┣ 📂.vscode
┣ 📂app
┣ .env
┗ ...
Put an environment variable in the .env
file
WEB_SERVER_PORT=5000
We’re going to load this WEB_SERVER_PORT
env variable from our source code, therefore it won't be hard coded. Now the only thing what remained is to loading the content of the .env
file in our app/config/app.js
.
You can access environment variable via process.env
built in property:
.gitignore
Finally it might be a good idea to put .env
file into .gitignore
to be customizable by each developer without messing with the git repository. To make it easier to create it, build a versioned .env.sample
file, which goes into the git repository.
Nodemon
Currently in our development environment, if we want verify code changes we stop the server, change the source code and starts the server. nodemon helps us by automatically restarting the application when a file change happens.
Install it as a development dependency.
npm install --save-dev nodemon
From now on you can use nodemon
as:
npx nodemon app/server.js
package.json scripts
In package.json
you can setup scripts, which allows you to run commands using npm run <script-name>
. Running nodemon
via a package.json
scripts looks like this:
I just created a dev
script and I can run it as:
npm run dev
NODE_ENV
The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production). One of the simplest things you can do to improve performance is to set NODE_ENV to “production.” — expressjs.com
By default, app is in development mode. You can change the environment by changing process.env.NODE_ENV
. For example in development environment logging is more verbose while in production environment there can be chached files (views).
cross-env
cross-env is a cross platform package to allow consistently setting environment variables in both Windows and Linux.
npm install --save-dev cross-env
We’re going to use this package to set the application environment via NODE_ENV
.
In the meantime I juse created a prod
script as well which uses node
to run our applcaition in production mode.
Source Code
Next chapter
Chapters
Adams Academy
theadamsacademy@gmail.com