How to scaffold ExpressJS server and test it
The absolute basics.
This is part 1 of multi-part series on express API.
Introduction
Express website introduces thusly—
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
But what is Node.js ? Glad you asked!
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting — running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser.
Simply put —
Node.js allows javascript to run outside of browser, thus allowing us to create a server using JS.
Express is a framework based on Node.js and adds more functionality and makes it easier for us to create a server.
Enough talk, Let’s Start!
(Before we really start, make sure you have node installed already
node --version
, else install it from here)
- Create a new folder which will hold our code —
mkdir express-demo && cd express-demo
- (Optionally) Initialize git for version control and add a
.gitignore
file —
git init
touch .gitignore
Add node_modules
and .env
to .gitignore
After this basic setup, time to go after it!
- Create a package.json file in the folder by
npm init -y
.
This will skip the interactive process for npm init
and create a basic package.json
file in the root folder.
Time to install dependencies —
We will use npm install
or npm i
to get the packages from npm registry and install them in our app.
- Start with the big one first!
npm i express
- We add another useful package
npm i dotenv
Dotenv package allows us to store and access valuable private information like credentials securely and easily.
- Some more packages
npm i — save-dev morgan nodemon
Morgan logs the network requests received by our API, and we can decide what details we need to see in the log.
Nodemon keeps the API running with updated code, every time we change something. So it always keeps the latest API available for us.
Now we have requisite packages to start writing our code.
- Create
src
folder and addapp.js
in it.
mkdir src
touch src/app.js
- Create a
.env
file to hold our environmental variables.
touch .env
Add the desired port to .env
- Add a command
start
topackage.json
—
“start”: “nodemon ./src/app.js”
- Update
app.js
with following code —
Here we use dotenv to fetch the port from .env
file and use it.
We require express and scaffold a server called app
. To keep the server running, we need app to listen on the 3000 port for any incoming network requests.
Enough coding, let’s see it in action already!
Run npm run start
on your terminal and you will see App listening on port 3000
Time to add other dependencies, (or middlewares) to app.js
which will now look like this —
Make sure you see App listening on port 3000 still on the terminal.
You have now successfully scaffolded an express API.
There is, however, no way to actually reach the API we just built. For that, we need to add endpoints to our API where clients can reach out and interact.
We will add GET /posts
route first.
Adding a route to our app
- Add a
routes
folder inside/src
folder and addposts.js
to it.
mkdir src/routes
touch src/routes/posts.js
We will use express router to set up our routes.
And we need to import it in app.js
to use it.
Now (assuming your nodemon is still running in terminal) if you visit localhost:3000/posts
in your browser, you will see You have hit GET /posts endpoint
in the tab.
You will also see GET /posts 200 2.252 ms — 32
in your nodemon log.
Instead of sending a string as response, we can send a JSON object as well.
Testing our app
As our app grows, it becomes important to make sure the functionality is working as intended. Automated testing is a fantastic tool for that.
npm i — save-dev chai mocha supertest
- Add a configuration file for mocha —
touch .mocharc.json
- Update “test” script in
package.json
with
”test”: “NODE_ENV=test NODE_NO_WARNINGS=1 mocha”
We will also add module.exports = app
at the bottom of our app.js
file to make it accessible to mocha.
- Create a
test
folder and addposts.spec.js
file in it —
mkdir test
touch test/posts.spec.js
.
Add the following code to it —
- Run the test
npm run test
It will go green!
In the next part, we will look at setting up database and connecting our API with it to persist data.
Here is Part 2.
Craft Academy is a Tech Education Provider that aims to bring new talent to the market and help to solve the shortage of tech workers. We are founded on the belief that modern development standards, agile methodologies, and business skills are fundamental for IT professionals. Our primary service is a 12-week coding bootcamp designed to provide individuals with a foundation of skills that allows them to enter the industry as junior developers.
With that foundation, our learners find employment in various industries or start their own businesses that bring new innovations to the market.
Would you like to know more about what we do? Follow us here on Medium, Facebook, Twitter or visit our website.