Base Express Application Preparation – Node.js API with TDD Tutorial

So far, we have generated the express app, installed the dependent packages, started the node server for this application and verified that the application is up and running as expected.

Deep Dive Into Generated Express App 

Let’s dive deeper into the generated base application and see what the files and directories are created. Also, we will review each of these files and directories to understand why they are created by default and its usage.

Let’s look at the generated express app’s files and directories.

1. bin directory

This directory contains the javascript file www which creates the node server and listens to the port 3000.

2. public directory

It contains few subdirectories: images, javascript, and stylesheets for all own static content files. All the files under this directory will be exposed to public access to serve those static content.

3. routes directory

Routes directory contains some sample routes javascript files. By default, it has index.js to render the index page when we access the http://localhost:3000 and users.js to demonstrate the resources can be obtained with additional routes configuration.

4. views directory

It is the directory where templates or HTML files can be hosted to serve the static or dynamic content when we use this app as a web or mobile application. Express supports various view engines to render the view content. To name a few view engines, Jade, Handlebars Pug, and other engines are available for us to use. You can check out the supported view engines with the below command.

express -h

5. apps.js file

This file is the heart of the express app as all the declaration and initialization of the application is happening here. The bin/www file imports and invokes this file to start the node server to render the express application. We will go over this file in detail in the further sections.

6. package.json file

This JSON file holds details about the application like name, version, start scripts, test scripts and also the dependencies for deployment and development. npm command-line utility reads this file to install or update any dependent packages.

7. package-lock.json file

This file keeps the dependency tree installation and its order. As we will add/modify the dependency package and its version over the course of the development process, this file keeps the records of those changes and maintains them. It helps during the subsequent installs by making sure that the packages are installed as described in this dependency tree and in that order.

Now, we have gone over the default content of the generated express app lets clean up the content to make it more relevant for the base app for API development.

Final Preparation Of Base App For API

Express is a flexible web framework, which provides features for complete web and mobile applications, with support for both front end and back end features. Since we are using this framework to develop and run the RESTful service, we wouldn’t need all the front-end related features like rendering the templates, HTML, stylesheets.

To clean up the express scaffolding app to prepare for our base app, we will have to remove some files, folders and some content from the files.

1. bin directory

This directory is needed as it’s the one that creates and runs the server for the RESTful service.

2. public directory

This directory will not be required for the API project as we will not expose any public static content.

3. routes directory

As we will be creating our own routes related files to expose the end-points of the API, we will not need this directory and its content too.

4. views directory

As the API exposes the endpoints it provides for the resources, and it doesn’t expose any static content like HTML and templates, we will remove this as well.

5. apps.js file

Even though this file contains both front-end and back-end related modules declarations and initializations, this is needed for the API tool. We will remove those front-end related scripts from this file and make it more robust for the API-only app.js file.

  • Remove the below lines as we removed the routes directory and its content.
     var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

app.use('/', indexRouter);
app.use('/users', usersRouter);
  • Remove the below lines as the API will not serve any static content or files.
     // view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));

After removing the above lines if you run the server and navigate to the link http://localhost:3000, you will end up with an error message on the browser along with some error stack trace. That’s because of the few more changes needed to be done at the apps.js.

Error: No default engine was specified, and no extension was provided.

Below changes will fix this error message.

  • Remove the below line as it still sends the response as the error page.
     res.render('error');
  • Add the below lines at the same place as the above-removed line. These new lines of code will send the error details as JSON response to the request if and when an error occurred. 
     res.json({
message: res.locals.message,
error: res.locals.error
});
  • Also, add the below lines above the error handling code blocks. It is replacing the index page that we just removed with the above cleanup task. Essentially this code block is reading from the package.json and sends the information as JSON response to the http://localhost:3000 request.
     app.get('/', function (req, res) {
var pkg = require(path.join(__dirname, 'package.json'));
res.json({
name: pkg.name,
version: pkg.version,
status: 'up'
});
});

6. package.json file

Since this describes and maintains the app details and dependencies, we will have to keep it intact so that API related details and dependencies are maintained for or reference and npm’s need.

Also, we will do some cleanup on this file too. Remove the below line from the dependencies list as we no longer need ‘jade’ for any view pages response. The versions of these packages could be different when you try this exercise.

"jade": "~1.11.0"

7. package-lock.json file

This file will be needed for both web & mobile applications and RESTful service too. So we will keep this file as it is.

Finally, after all these cleanup tasks and changes are complete if you open up the link http://localhost:3000 in the browser, you will be able to see the below JSON content as per out changes.

With this testing, we are done with the preparation of the base app for API development. We will be using this base app for further development with API related code.

You can check out the below link to the source code for this step in GitHub.

GitHub – Step 02 – Final Preparation Of Base App For API 

Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.

This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.