Files
ClassContent/历届学生/朱立伟/项目案例/weather/app.js
2024-09-27 02:06:13 +08:00

30 lines
934 B
JavaScript
Executable File

const express = require('express')
const app = express()
const request = require('request');
const URL = {
index: 'http://v.juhe.cn/weather/index',
key: '62a1ba7a26c06fa3720a8e91c103d5b5'
}
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
// http://127.0.0.1:3000/weather/index
app.get('/weather/index', function (req, res) {
var cityname = req.query.cityname;
console.log(`${URL.index}?format=2&cityname=${encodeURI(cityname)}&key=${URL.key}`);
request(`${URL.index}?format=2&cityname=${encodeURI(cityname)}&key=${URL.key}`, function (error, response, body) {
res.json(JSON.parse(body))
});
})
app.listen(3000, function () {
console.log('node app listening on port 3000!')
})