我有一个从API获取数据的功能。我现在想将程序逻辑外包给服务,以保持控制器的整洁。我得到有关异步/等待的数据,不幸的是,我不知道如何将其外包给服务?有人有主意吗?这是我的homeController.js:const ispwrapper = require('../lib/ispwrapper');require('dotenv').config();const BASE_URL = process.env.API_BASE_URL;const OPTIONS = { username: process.env.API_USERNAME, password: process.env.API_PASSWORD};const renderHome = async (req, res) => { let domain = [], message = ''; try { let a = new ispwrapper.ISPConfig(BASE_URL, OPTIONS); const response = await a.getDataByPrimaryId('sites_web_domain_get', { active: 'y' }); for (let i = 0; i < response.length; i++){ domain.push(response[i].domain); } } catch(err) { message = 'Error when retriving domains from API'; } finally { res.render('home', { title: 'ISPConfig', heading: 'Welcome to my ISPConfig Dashboard', homeActive: true, domain, message }); }};module.exports = { renderHome};我的homeService.js:const ispwrapper = require('../lib/ispwrapper');require('dotenv').config();const BASE_URL = process.env.API_BASE_URL;const OPTIONS = { username: process.env.API_USERNAME, password: process.env.API_PASSWORD};const getDomains = async () => {// i have no idea how use my renderHome() logic here};module.exports = { getDomains};
添加回答
举报
0/150
提交
取消