Я бы порекомендовал вам использовать минимальное MVC Express App:
При app.js
вы запускаете сервер:
var express = require('express');
var app = express();
var index = require('../controllers/index');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.get('/', function (req, res) { // index url of your web app. Will call to index controller
index
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
При controlles/index.js
вы указываете логику своей основнойurl (получить данные вашего API и преобразовать их в представление для преобразования в HTML):
const axios = require('axios');
const asyncUtil = fn =>
function asyncUtilWrap (req, res, next, ...args) {
const fnReturn = fn(req, res, next, ...args)
return Promise.resolve(fnReturn).catch(next)
}
module.exports = {
index: asyncUtil(async (req, res, next) => {
let users = await axios(url)
res.render('index', {users})
})
}
Вы зададите HTML в представлении pug на views/index.pug
, которое преобразует его в HTML:
table
thead
tr
th Id
th Name
th Username
tbody
each user in users // iterate through users json
tr
td #{user.id}
td #{user.name}
td #{user.username}