0
當用戶輸入城市名稱,或者可以使用您的IP地址獲取當前位置和當前天氣時,此應用將獲取當前天氣。無法從獲取請求獲取數據時關閉節點上的快速服務器
問題:當用戶鍵入一個不存在的城市時,如「jklsafbhjbdsfjhbf」,失敗消息按預期淡入,但隨後服務器關閉,用戶無法再進行更多天氣請求重新啓動終端中的服務器。
我認爲這個問題的地方所在的文件weather.js,或可能在路上,我處理的前端我的GET請求在weatherFetcher.js或後端在我的諾言內在app.js
需要幫助解決這個問題,以便服務器不關閉,用戶可以繼續提出天氣請求。
app.js
var express = require('express');
var weather = require('./public/js/weather.js');
var location = require('./public/js/location.js');
var app = express();
var PORT = process.env.PORT || 3000;
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.send(__dirname + '/public/index.html');
});
app.listen(PORT, function() {
console.log('Express Server Started on port ' + PORT);
});
app.get('/location', function(req, res) {
var city = req.query.city;
weather(city).then(function (currentWeather) {
res.json(currentWeather);
});
});
app.get('/guessweather', function(req, res) {
location().then(function (guessedLocation) {
weather(guessedLocation).then(function (guessedWeather) {
res.json(guessedWeather);
});
});
});
weather.js
var request = require('request');
module.exports = function (location) {
return new Promise(function (resolve, reject) {
var encodedLocation = encodeURIComponent(location);
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + encodedLocation + '&units=imperial&appid=2de143494c0b295cca9337e1e96b00e0';
if (!location) {
return reject('No location provided');
}
request({url: url, json: true}, function (error, response, body) {
if (error) {
return reject('Unable to fetch weather.');
} else {
return resolve('The current temperature in ' + body.name + ' is ' + body.main.temp + ' degrees farenheit with humidity at ' + body.main.humidity + '%.');
}
});
});
};
location.js
var request = require('request');
module.exports = function() {
return new Promise(function (resolve, reject) {
var url = 'http://ipinfo.io';
request({
url: url,
json: true
}, function (error, response, body) {
if (error) {
return reject('Unable to fetch location.');
} else {
console.log(body.city);
return resolve(body.city);
}
});
});
}
個weatherFetcher.js
$(document).ready(function() {
$('#success').hide();
$('#failure').hide();
$('#noCity').hide();
$('#findMyWeather').click(function(event) {
$('#success').hide();
$('#failure').hide();
$('#noCity').hide();
var $cityName = $('#city').val();
if (typeof $cityName === 'string' && $cityName.length > 0) {
$.get('/location?city=' + $cityName, function (data) {
}).done(function (data) {
$("#success").html(data).fadeIn();
$("#success").prepend('<p id="cityTitle" class="lead">'+ $cityName +'</p>');
$('#city').value = ""; //This doesn't seem to be working
}).fail(function() {
$("#failure").fadeIn();
});
} else {
$("#noCity").fadeIn();
}
});
$('#findLocalWeather').click(function(event) {
$('#success').hide();
$('#failure').hide();
$('#noCity').hide();
$.get('/guessweather', function (data) {
if (data == "") {
$("#failure").fadeIn();
} else {
$("#success").html(data).fadeIn();
}
});
});
});
的index.html
<!doctype html>
<html>
<head>
<title>Weather Scraper</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--jQuery & Bootstrap-->
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<!--javascript-->
<script src="./js/weatherFetcher.js"></script>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="./css/weatherStyles.css">
</head>
<body>
<div class="container">
<div class="row">
<div id="heading" class="col-md-6 col-md-offset-3 center">
<div id="pageIntro">
<h1 class="middle">Current Weather</h1>
<p class="lead center marginTop">Enter a city to get the current weather or click Local Weather</p>
</div>
<div class="form-group">
<input type="text" class="form-control marginTop" name="city" id="city" placeholder="Eg. London, Venice, Atlanta, Ho Chi Minh City...">
</div>
<button id="findMyWeather" class="btn btn-primary btn-lg marginTop">Get Weather by City</button>
<button id="findLocalWeather" class="btn btn-primary btn-lg marginTop">Get Your Local Weather</button>
<div id="alerts">
<div id="success" class="alert alert-info marginTop"></div>
<div id="failure" class="alert alert-danger marginTop">Could not find weather data for that city. Please try again.</div>
<div id="noCity" class="alert alert-danger marginTop">Please enter a city</div>
</div>
</div>
</div>
</div>
</body>
</html>
我添加了這個錯誤回調,但服務器仍然關閉並在控制檯上引發此消息。 /Users/marcushurney/Desktop/node-course/Weather-App/public/js/weather.js:25 \t \t \t \t返回解析(+ body.name + '在當前溫度' '是' +體.main.temp +'degree farenheit with humidity at'+ body.main.humidity +'%。'); \t \t \t \t^ 類型錯誤:無法讀取屬性「臨時」的未定義 – Mjuice
你能後的console.log(體)的結果? –
{座標:{經度:-84.39,緯度:33.75}, 天氣: [{ID:800, 主: '清除', 描述: '天空晴朗', 圖標: '01D'}], 基: '站', 主: {溫度:24.03, 壓力:1031, 溼度:73, temp_min:19.99, temp_max:28。4}, 能見度:16093, 風:{速度:3.24,DEG:30}, 雲:{所有:1}, DT:1451999483, SYS: {類型:1, ID:760, 消息:0.0086, 國家: '美國', 日出:1451997779, 日落:1452033799}, ID:4180439, 名稱: '亞特蘭大', 鱈魚:200} – Mjuice