使用loopback创建数据模型时,会自动生成js和json两个文件 (文件名转换成小写) 位置:common/models/coffee-shop.json & common/models/coffee-shop.js
1.修改coffee-shop.js
新增status方法
module.exports = function(CoffeeShop) { /方法名/ CoffeeShop.status = function(cb) { var currentDate = new Date(); var currentHour = currentDate.getHours(); var OPEN_HOUR = 6; var CLOSE_HOUR = 20; console.log('Current hour is ' + currentHour); var response; if (currentHour > OPEN_HOUR && currentHour < CLOSE_HOUR) { response = 'We are open for business.'; } else { response = 'Sorry, we are closed. Open daily from 6am to 8pm.'; } cb(null, response); }; CoffeeShop.remoteMethod( 'status', { http: {path: '/status', verb: 'get'}, returns: {arg: 'status', type: 'string'} } ); };
2.启动
必须重启 >slc run
打开 http://localhost:3000/explorer 可见 新增的status方法