Beginners Guide To Building Real-World Microservices With Node - Js
Beginners Guide To Building Real-World Microservices With Node - Js
js
← All Articles
Beginners Guide to
Building Real-World
Microservices with Node.js
Mike Mackrory 12 SEPTEMBER 2018
Initial Steps
You’ll need to have Node.js installed on your workstation
for this example. I’m using version 8.11, and you can
download the latest version for your operating system from
Nodejs.org. The Node.js installation includes NPM, which
is the Node.js package manager. We’ll use NPM to
bootstrap the project, install dependencies and execute
the service. (You can follow along with this example or
view the completed example in this GitHub repository.)
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 2/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
We’ll start by initializing the project using npm init. Run this
Products Blog Try it free
command in the root folder for the project. The command
walks you through the creation of the package.json file,
which is the foundation for the project. If you’re not sure of
an answer, accept the default, and we can update it later if
necessary. We’ll be using the name and version a little
later.
$ npm init
One last thing before we delve into the code. Let’s look at
the structure of how it’s all going to fit together. There are
two files and a folder created by the npm init command.
These are package.json, package-lock.json, and
node_modules. When we installed the express and
request packages, their dependencies were downloaded
and saved in node_modules.
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 3/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
routes(app);
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 4/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
app.listen(port, function() {
Products Blog Try it free
console.log('Server started on port: ' + port);
});
'use strict';
Products Blog Try it free
module.exports = function(app) {
app.route('/about')
.get(controller.about);
app.route('/distance/:zipcode1/:zipcode2')
.get(controller.get_distance);
};
This function adds two routes to the app. The first route
listens for GET requests on the /about endpoint. These
requests are handled by the about function in the
controller. The second route listens for GET requests on
the /distance endpoint. The get_distance function in the
controller handles these requests. Two parameters are
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 6/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
'use strict';
var controllers = {
var aboutInfo = {
name: properties.name,
version: properties.version
res.json(aboutInfo);
},
if (err)
res.send(err);
res.json(dist);
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 7/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
});
Products Blog Try it free
},
};
module.exports = controllers;
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 8/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
We’re ready for the final piece of the puzzle. This file
Products Blog Try it free
handles the call to a third-party API. We’ll use the distance
API provided by ZipCodeAPI.com. (You need an API key
to use this, and it is free if you register. You can also use
the key from the example if you want to test your service,
but this key frequently expires during the day.)
"hkCt1nW1wF1rppaEmoor7T9G4ta7R5wFSu8l1dokNz8y53gGZHDneWWVosbEYi
rC";
var distance = {
request(zipCodeURL + apiKey
+ req.params.zipcode2 + '/mile',
response = JSON.parse(body);
res.send(response);
} else {
console.log(response.statusCode +
response.body);
res.send({distance: -1});
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 9/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
}
Products Blog Try it free
});
};
module.exports = distance;
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 10/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
$ npm install
$ npm start
http://localhost:3000/about
http://localhost:3000/distance/84010/97229
http://localhost:3000/distance/84010/92001
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 11/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
Related Content:
Kubernetes: Orchestrate your Node.js
(micro)services
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 12/13
12/23/2019 Beginners Guide to Building Real-World Microservices with Node.js
← All Articles
Products Blog Try it free
Containers
Company
Jobs
Security
Contact
Products
Rails
Node
Maestro
Skycap
Open Source
Status
Status
Uptime
Connect
Twitter
GitHub
Connect
Documentation
Community Forum
Slack Channel
RealScale
https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/ 13/13