我使用express和pug文件创建了一个节点应用程序。快递应用程序调用并侦听端口3000并呈现pug文件。我有一个从api中获取信息的功能,我希望能够使用这些信息并使用pug文件进行打印。
这是我的app.js文件
'use strict';
const express = require('express')
const app = express();
const pug = require('pug');
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.get('/', (req, res) => {
res.render('index');
});
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
res.locals.error = err;
res.status(err.status);
res.render('error');
});
app.listen(3000, () => {
console.log('The application is running on localhost:3000!')
});
这是我希望从pug文件中获取信息的函数。
const printWeather = (weather) => {
let message =`The weather in ${weather.location.city} is currently ${weather.current_observation.weather}`;
message += ` Current temperature is ${weather.current_observation.temp_c}C`;
message += ` It currently feels like ${weather.current_observation.feelslike_c}C`;
message += ` The wind speed is currently ${weather.current_observation.wind_mph}mph`;
message += ` The UV is currently ${weather.current_observation.UV}`;
message += ` The humidity is currently ${weather.current_observation.relative_humidity}`;
message += ` The wind direction is currently in the ${weather.current_observation.wind_dir}`;
message += ` The pressure is currently ${weather.current_observation.pressure_mb}hPa`;
message += ` The idibility is currently ${weather.current_observation.visibility_km}km`;
}
function get(query){
const readableQuery = query.replace('_', ' ');
try {
const request = https.get(`https://api.wunderground.com/api/${api.key}/geolookup/conditions/q/${query}.json`, response => {
if(response.statusCode === 200){
let body = "";
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
try{
const weather = JSON.parse(body);
if (weather.location){
printWeather(weather);
} else {
const queryError = new Error(`The location "${readableQuery}" was not found.`);
printError(queryError);
}
} catch (error) {
printError(error);
}
});
} else {
const statusCodeError = new Error(`There was an error getting the message for ${readableQuery}. (${http.STATUS_CODES[response.statusCode]})`);
printError(statusCodeError);
}
});
这是帕格文件。
doctype html
html(lang="en")
head
title Weather App
body
h1 Weather App
h2 #{message}
我似乎无法从pug文件中获取信息。
如果您想再查看我的代码,请告诉我们。
我知道这可能不是创建和运行我的应用程序的最佳方式,但我是节点,表达和哈巴狗的初学者,这只是我试图自己学习一些代码。
答案 0 :(得分:1)
你会想做类似的事情:
app.get('/', (req, res) => {
// this assumes that `getWeather` returns a promise or is an async function
getWeather(req)
.then(weather => {
// make sure the `printWeather` function actually returns the message
const message = printWeather(weather);
res.render('index', { message });
});
});
答案 1 :(得分:0)
render
接受第二个参数,该参数接受要传递给视图的数据:
const get = (query, res, req) => {
// your query code... not shown to save space
// lets start from inside the try block where you parse the body
const weather = JSON.parse(body);
// now instead of calling printWeather(weather);
// you need to take the json data from the api call
// and pass this json to the view
if (weather.location) {
res.render('index', { weather: weather, error: null } /* passed to index.pug */)
} else {
res.render('index', { weather: null, error: `the location was not found.` } /* passed to index.pug */)
}
}
app.get('/', function (req, res) {
get(`enter_a_query_here`, req, res);
})
然后你可以在index.pug中使用这个数据。
doctype html
html(lang="en")
head
title Weather App
body
h1 Weather App
h2 Data: #{weather.current_observation.weather}
p Error: #{error}