Javascript NodeJS发出一个新的GET请求以从服务器获取数据
嗯, 我正在为一个在线课程做一个项目,我需要对该项目做一个更改。我不太明白代码审查员说我做错了什么。他的评论是: 在这里,您需要启动一个新的GET请求来从服务器获取数据 GET和POST请求有特定的用途 获取从服务器/db获取数据的请求 POST用于在服务器/db中创建新数据 这些请求必须完成它们设计的任务,而不是其他任务 这是我的代码的问题区域:Javascript NodeJS发出一个新的GET请求以从服务器获取数据,javascript,node.js,express,Javascript,Node.js,Express,嗯, 我正在为一个在线课程做一个项目,我需要对该项目做一个更改。我不太明白代码审查员说我做错了什么。他的评论是: 在这里,您需要启动一个新的GET请求来从服务器获取数据 GET和POST请求有特定的用途 获取从服务器/db获取数据的请求 POST用于在服务器/db中创建新数据 这些请求必须完成它们设计的任务,而不是其他任务 这是我的代码的问题区域: let postData = async(url = '', data = {})=>{ console.log(data);
let postData = async(url = '', data = {})=>{
console.log(data);
let temp = data.main.temp;
let zip = document.getElementById('zip').value;
let feelings = document.getElementById('feelings').value;
let date = newDate;
let response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( { temp, zip, feelings, date }),
});
try {
const newData = await response.json();
console.log(newData);
document.getElementById("date").innerHTML = newData.date;
document.getElementById("temp").innerHTML = newData.temp;
document.getElementById("content").innerHTML = newData.feelings;
return newData
}catch(error) {
console.log("error", error);
}
}
这是我的完整代码:
app.js:
let apiURL = 'http://api.openweathermap.org/data/2.5/weather?id=524901&APPID=' + apiKey + '&zip=';
const endURL = ',us';
// Create a new date instance dynamically with JS
let d = new Date();
let newDate = d.getMonth()+'.'+ d.getDate()+'.'+ d.getFullYear();
document.getElementById('generate').addEventListener('click', performAction);
let content = document.getElementById('feelings').value;
function performAction(e){
let zip = document.getElementById('zip').value;
let url = apiURL + zip + endURL;
apiCall(url)
.then(async function(data){
console.log(data);
let res = await postData('/', data);
console.log(res);
});
};
const apiCall = async (url) =>{
const res = await fetch(url);
try {
const data = await res.json();
console.log(data)
return data;
} catch(error) {
console.log(error)
}
};
let postData = async(url = '', data = {})=>{
console.log(data);
let temp = data.main.temp;
let zip = document.getElementById('zip').value;
let feelings = document.getElementById('feelings').value;
let date = newDate;
let response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( { temp, zip, feelings, date }),
});
try {
const newData = await response.json();
console.log(newData);
document.getElementById("date").innerHTML = newData.date;
document.getElementById("temp").innerHTML = newData.temp;
document.getElementById("content").innerHTML = newData.feelings;
return newData
}catch(error) {
console.log("error", error);
}
}
server.js:
let projectData = {};
// Require Express to run server and routes
const express = require('express');
// Start up an instance of app
const app = express();
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
const bodyParser = require('body-parser');
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors());
// Cors for cross origin allowance
// Initialize the main project folder
app.use(express.static('website'));
// Setup Server
const port = 8000;
const server = app.listen(port, listening);
function listening(){
console.log(`running on localhost: ${port}`);
};
app.get('/weather', getData);
function getData(req, res){
res.send(projectData)
console.log(projectData)
};
app.route('/')
.get(function (req, res) {
res.sendFile('index.html', {root: 'website'})
})
.post(getWeather);
function getWeather(req, res){
console.log(req.body);
projectData = req.body;
console.log(projectData);
res.status(200).send(projectData);
};
我不知道这些变化会是什么样子,因为我按照他们在课程中教的相同结构编写了这段代码。任何帮助都将不胜感激
非常感谢,
Mike我认为您正在尝试从API获取数据,所以他试图说,当您尝试从API获取数据时,您需要获取请求。我找到了答案。我需要编写这个函数:
const updateUI = async () =>{
const res = await fetch('/weather');
try {
const allData = await res.json();
console.log(allData)
document.getElementById("date").innerHTML = allData.date;
document.getElementById("temp").innerHTML = allData.temp;
document.getElementById("content").innerHTML = allData.feelings;
return allData
} catch(error) {
console.log(error)
}
};
并将我的postData函数编辑为:
let postData = async(url = '', data = {})=>{
console.log(data);
let temp = data.main.temp;
let zip = document.getElementById('zip').value;
let feelings = document.getElementById('feelings').value;
let date = newDate;
let response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( { temp, zip, feelings, date }),
});
try {
updateUI()
/* const newData = await response.json();
console.log(newData);
document.getElementById("date").innerHTML = newData.date;
document.getElementById("temp").innerHTML = newData.temp;
document.getElementById("content").innerHTML = newData.feelings;
return newData
*/
}catch(error) {
console.log("error", error);
}
}
你能解释一下API的用途吗?考虑到您在这里向我们展示的代码,您试图做什么还不是100%清楚。不应该有太多的迷雾,让你进入正确的方向。我猜您正在尝试设计一个API,让用户从
http://api.openweathermap.org/
,对吗?另外,我有点搞不清楚你是如何使用文档的
这里是app.js
客户端文件吗?