MicroMono 代码示例 介绍
MicroMono 可以使用整体风格编写微服务。MicroMono 是一个使用 monolithic 风格开发微服务的框架,允许你切换和混合 微服务/整体
风格,不需要修改代码。之前两者是通过 VS 连接,现在使用 micromono 可以很好的同时处理两者。
MicroMono 包括 3 部分:
MicroMono 包含两种类型的组件:
代码示例
定义一个服务
// Require micromono and get the Service base class var Service = require('micromono').Service; // Subclass Service class to define your service // (Backbone/Ampersand style inheritance) var SimpleHttpService = Service.extend({ // `route` is the object where you define all your routing handlers route: { 'get::/hello/:name': function(req, res) { // Basically, this handler function will be directly attached to // internal express instance created by micromono. So, any express // route handler Could be ported to micromono without any modification. var name = req.params.name; res.send('Hello, ' + name); } } }); The 'get::/hello/:name': function(req, res){...} part in above example equivalents to: var app = express(); app.get('/hello/:name', function(req, res){ var name = req.params.name; res.send('Hello, ' + name); });
服务初始化
var bodyParser = require('body-parser'); var Service = require('micromono').Service; var MongoClient = require('mongodb').MongoClient; module.exports = Service.extend({ // initialization function takes no arguments init: function() { // get the internal express instance var app = this.app; // use a middleware app.use(bodyParser.json()); var self = this; // create a new promise instance var promise = new Promise(function(resolve, reject){ // do the async operation (connect) MongoClient.connect('127.0.0.1', function(err, db){ if (err) { // reject the promise if there's an error reject(err); return; } self.db = db; // resolve when done resolve(); }); }); // init function should return a promise no matter what return promise; } });
MicroMono 代码示例 官网
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。