Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在express.js中使用response.send()设置发送数据的样式_Javascript_Node.js_Express - Fatal编程技术网

Javascript 如何在express.js中使用response.send()设置发送数据的样式

Javascript 如何在express.js中使用response.send()设置发送数据的样式,javascript,node.js,express,Javascript,Node.js,Express,我想在app.post()方法中设置发送回的数据的样式。我怎样才能做到这一点 这是app.post()代码: app.post(“/”,函数(req,res){ const query=req.body.cityName; const cityName=query.charAt(0.toUpperCase()+query.slice(1); const apiKey=“fc2f5f2ba2fe9d109d021ef0f57ef03d”; const unit=“公制”; 常量url=”https

我想在app.post()方法中设置发送回的数据的样式。我怎样才能做到这一点

这是app.post()代码:

app.post(“/”,函数(req,res){
const query=req.body.cityName;
const cityName=query.charAt(0.toUpperCase()+query.slice(1);
const apiKey=“fc2f5f2ba2fe9d109d021ef0f57ef03d”;
const unit=“公制”;
常量url=”https://api.openweathermap.org/data/2.5/weather?q=“+cityName+”&appid=“+apiKey+”&units=“+units;
https.get(url、函数(响应){
console.log(响应.状态码);
响应.on('data',函数(data){
const weatherData=JSON.parse(数据);
常数温度=weatherData.main.temp;
const weatherDescription=weatherData.weather[0]。description;
const icon=weatherData.weather[0]。图标;
常量imageURL=”http://openweathermap.org/img/wn/“+图标+”@2x.png”;
res.write(“城市名称+”中的温度为“+temp+”摄氏度”);
res.write(“当前天气为”+天气描述+”);
res.write(“”);
res.send();
})
});
})

使用内联样式将一如既往地成为一种选择。 或者,当返回结果时,可以使用单独的HTML文件进行渲染,并使用其中的结果值。 我要说的是有一个单独的HTML文件('weather.HTML'),然后将数据呈现到其中。 weather.html将如下所示:

<h1>temperature in {%CITY%} is {%temperature%}</>

看起来是这样的。试试看。

我认为写这样的代码是一种可怕的经历。您需要编写模板并返回样式化的模板。但在节点中编写内部代码时,可以使用内部CSS对其进行样式设置。像这样的。
<h1>temperature in {%CITY%} is {%temperature%}</>
const fs = require('fs');
response.on('data', function(data){
  const weatherData = JSON.parse(data);
  const temp = weatherData.main.temp;
  const weatherDescription = weatherData.weather[0].description;
  const icon = weatherData.weather[0].icon;
  const imageURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png";

  fs.readfile(`${__dirname}/weather.html`,'utf-8',(err,data)=>{
   let output = data.replace('{%CITY%}', data.city)
})