未加星标

Build your first Node.js microservice

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

A microservice is a single self-contained unit which, together with many others, makes up a large application. By splitting your app into small units every part of it is independently deployable and scalable, can be written by different teams and in different programming languages and can be tested individually.

micro is a tiny (~100 LoC) module that makes writing a microservice in Node.js a joy. It’s easy to use and super fast. No matter if you’ve used Node.js before or not, after this post you’ll be able to write your own microservices!

The Setup

There are two tiny steps needed for the setup, first we need to install micro :

npm install -g micro

I’m installing it globally to make sure we get access to the micro command. If you know how to usenpm scripts, feel free to use those instead!

And second we need to create a file that will contain our microservice. Let’s call it index.js :

touch index.js The First Steps

Our index.js file needs to export a single function, which micro will pass the incoming request and a response object to:

module.exports = function (request, response) { // Your microservice here }

The main function we’ll use from micro is send , which allows us to send a response back to the client. Let’s require it and send a simple “Hello World”, no matter what the request is:

const { send } = require('micro') module.exports = function (request, response) { send(response, 200, 'Hello World! :wave:') }

send takes the response we want to send as the first argument, the HTTP status code the response should have as the second argument and some body (can also be JSON) as the third argument.

Starting our microservice is just one command away:

$ micro index.js Ready! Listening on http://0.0.0.0:3000

Open the page in your browser and this is what you’ll see:


Build your first Node.js microservice
Building something useful

Since that’s quite boring, let’s build something useful! We want to make a microservice which remembers how many times a certain path has been requested. So when the first request to /foo comes in, it should return 1 . When another request to /foo comes in, it should return 2 , and so on.

The first thing we need is the pathname of the requested URL. We get the URL from request.url , and we’ll use Node.js core url module (so you don’t need to install it) to parse it.

Let’s require the url module and parse the requested URL to get the pathname :

const { send } = require('micro') const url = require('url') module.exports = function (request, response) { const { pathname } = url.parse(request.url) console.log(pathname) send(response, 200, 'Hello World! :wave:') }

Restart the microservice (press CTRL+C , then enter micro index.js again) and try it out. Requesting localhost:3000/foo logs /foo to the terminal, and requesting localhost:3000/bar logs /bar .

Now that we have the pathname, the last step is to save the number of requests to that specific pathname. Let’s create a global object called visits , which will be responsible for saving all the visits:

const { send } = require('micro') const url = require('url') const visits = {} module.exports = function (request, response) { const { pathname } = url.parse(request.url) send(response, 200, 'Hello World! :wave:') } Every time a request comes in we check if visits[pathname] already exists. If it does, we increment the views and send them back to the client. If it doesn’t, we set it to 1 and send that back to the client. const { send } = require('micro') const url = require('url') const visits = {} module.exports = function (request, response) { const { pathname } = url.parse(request.url) if (visits[pathname]) { visits[pathname] = visits[pathname] + 1 } else { visits[pathname] = 1 } send(response, 200, `This page has ${visits[pathname]} visits!`) }

Restart the service again, open localhost:3000/foo in your browser and refresh a bunch of times. This what you’ll see:


Build your first Node.js microservice

This is basically how I ended up building micro-analytics in a few hours. It’s the same concept, with a few more features, and once I knew what I was building the code to build it was easy.

Persisting Data

Something you might notice about our service is that the data is deleted whenever we restart the server. We don’t save the visits to a database, they solely exists in memory. Let’s fix that!

We’ll use level , a simple file-based key-value storage, to persist data across server restarts. micro has built-in support for async/await , which makes asynchronous code look beautiful. The issue is that level is callback based, not Promise based. :confused:

As always, npm has the modules we need. Forbes Lindesay wrote then-levelup , which allows us to promisify level . Don’t worry if you don’t understand all that, you’ll see what it looks like very soon!

Let’s install these modules:

npm install level then-levelup

To create our database we require level and tell it where to save the database and that we want the value storage to be JSON encoded. (so we can store numbers) We wrap that database in the function exported by then-levelup to promisify it and export an async function instead of a normal one so we can use the await keyword:

const { send } = require('micro') const url = require('url') const level = require('level') const promisifiy = require('then-levelup') const db = promisify(level('visits.db', { valueEncoding: 'json' })) module.exports = async function (request, response) { /* ... */ } The two functions we’ll need from our database are db.put(key, value) to save some data (that’s kinda like visits[pathname] = x ) and db.get(key) to get the data back out again! (that’s kinda like const x = visits[pathname] )

First, we want to see if there are current visits for that pathname in the database. We do this with db.get(pathname) and wait for that to finish with the await keyword:

module.exports = async function (request, response) { const { pathname } = url.parse(request.url) const currentVisits = await db.get(pathname) }

If we didn’t put await there, currentVisits would be a Promise and the function would go on before we have the value from the database ― not what we want!

Contrary to before, if there are no current visits db.get will throw a “NotFoundError”. We have to catch that with a try/catch blockand db.put the initial value for a path, which is 1 , if that happens:

/* ... */ module.exports = async function (request, response) { const { pathname } = url.parse(request.url) try { const currentVisits = await db.get(pathname) } catch (error) { if (error.notFound) await db.put(pathname, 1) } }

To finish it off we need to increment the current visits when we already have visits and send back a response:

/* ... */ module.exports = async function (request, response) { const { pathname } = url.parse(request.url) try { const currentVisits = await db.get(pathname) await db.put(pathname, currentVisits + 1) } catch (error) { if (error.notFound) await db.put(pathname, 1) } send(response, 200, `This page has ${await db.get(pathname)} visits!`) }

That’s everything we need to do! Now the visits of a page are persisted across server restarts, and written to the vists.db file. Try it out by restarting the service, opening localhost:3000/foo a bunch of times, then restarting the service again and going to the same page. You’ll see that your previous visits are still counted, even though you restarted the service.

Congratulations, you just built a page counter in 10 minutes flat! :tada:

That’s the power of small, focussed modules in Node.js: Instead of having to mess around with primitives directly, we just write our app.

Liked this post? Sign up for the weekly newsletter!

Be the first to know when a new article is posted and get an inside scoop on the most interesting news and the freshest links of the week. (I hate spam just as much as you do, so no spam, ever. Promise.)

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.js
分页:12
转载请注明
本文标题:Build your first Node.js microservice
本站链接:http://www.codesec.net/view/522991.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)