gpt4 book ai didi

javascript - 如何使用node和express将javascript函数打印到pug文件

转载 作者:行者123 更新时间:2023-12-03 02:22:02 24 4
gpt4 key购买 nike

我使用express和pug文件创建了一个 Node 应用程序。 Express 应用程序调用并监听端口 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);
}
});

这是 pug 文件。

doctype html
html(lang="en")
head
title Weather App
body
h1 Weather App
h2 #{message}

我似乎无法从 pug 文件中获取要显示的信息。

如果您想查看更多我的代码,请告诉我。

我知道这可能不是创建和运行我的应用程序的最佳方式,但我是 Node、Express 和 pug 的初学者,这只是我试图自己学习一些代码。

最佳答案

你会想要做类似的事情:

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 });
});
});

关于javascript - 如何使用node和express将javascript函数打印到pug文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49115696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com