我的代码使用来自外部网站的数据响应请求 - 它从互联网上检索数据,该数据与任何响应打包在一起。
我是一名javascript初学者,不习惯异步编程。有人可以告诉我,如果我这样做是正确的:
var Hapi = require('hapi');
var Handlebars = require('handlebars');
var ical = require('ical');
// Set up server here:
// ...
// Get data from the web:
var ical_url = 'url_here';
function fmt_events(data){
var eventData = {events:[]}
// format data and push onto eventData.events
return eventData;
}
// here is where I need feedback:
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
function events_callback(err, data) {
var eventData = fmt_events(data);
reply.view('index', {
title: 'Title',
events: eventData
});
}
ical.fromURL(ical_url, {}, events_callback);
}
});