javascript - 获取加载失败 : POST "http://localhost:3000/weather"

标签 javascript

终端错误:

> weather-app@1.0.0 devStart
> nodemon server.js

[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
node:events:355
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1310:16)
    at listenInCluster (node:net:1358:12)
    at Server.listen (node:net:1445:7)
    at Function.listen (/home/ivan-ilyich/Documents/dev/weather-app/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/ivan-ilyich/Documents/dev/weather-app/server.js:22:5)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1337:8)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
^C
ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ stop

Command 'stop' not found, but there are 18 similar ones.

ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ npm stop devStart
npm ERR! missing script: stop

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ivan-ilyich/.npm/_logs/2021-02-26T14_02_05_736Z-debug.log
ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ ^C
ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ ^C
ivan-ilyich@ivan-Ilyich:~/Documents/dev/weather-app$ npm run devStart

> weather-app@1.0.0 devStart
> nodemon server.js

[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
node:events:355
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1310:16)
    at listenInCluster (node:net:1358:12)
    at Server.listen (node:net:1445:7)
    at Function.listen (/home/ivan-ilyich/Documents/dev/weather-app/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/ivan-ilyich/Documents/dev/weather-app/server.js:22:5)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1337:8)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...

我是编程新手,也许我的问题很愚蠢,但我没有从类似问题中找到答案,所以我会尝试一下并发布自己的答案(抱歉我的英语不好,这不是我的母语). 我正在做一个小型教程天气应用程序项目,但我被迫以“我的”方式进行,并且我迷失了服务器和客户端的连接。 我不断收到该错误:获取加载失败:“http://localhost:3000/weather”

客户端:

const searchElement = document.querySelector('[data-city-search]')
const searchBox = new google.maps.places.SearchBox(searchElement)

searchBox.addListener('places_changed', () => {
    const place = searchBox.getPlaces()[0]
    if (place == null) {
        return
    }
    const latitude = place.geometry.location.lat()
    const longitude = place.geometry.location.lng()
    fetch('/weather', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify({
            latitude: latitude,
            longitude: longitude
        })
    })
    .then(res => res.json())
    .then(data => {
        console.log(data);
        // setWeatherData(data, place.formatted_address)
    })
})

服务器端:

if ( process.env.NODE_ENV !== 'production' ) {
    require('dotenv').config()
}

const OPEN_WEATHER_API_KEY = process.env.OPEN_WEATHER_API_KEY
const express = require('express')
const app = express()
const axios = require('axios')

app.use(express.json())
app.use(express.static('public'))

app.post('/weather', (req, res) => {
    const url = `api.openweathermap.org/data/2.5/weather?lat=${req.body.lat}&lon=${req.body.lon}&appid=${OPEN_WEATHER_API_KEY}`
    axios({
        url: url,
        responseType: 'json'
    })
    .then(data => res.json(data.main))
})

app.listen(3000, () => {
    console.log('Server Started!');
})

NEW ERROR

最佳答案

只需将 fetch('/weather', { 更改为 fetch('http://localhost:3000/weather', { 您在端口 3000 上托管您的 api 并尝试向端口 80 发送 POST 请求

关于javascript - 获取加载失败 : POST "http://localhost:3000/weather",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66377785/

相关文章:

php - 基于 php 下拉表单的重定向

javascript - 使用该整数和当前月份将整数值转换为日期

javascript - Jquery中的.data()总是返回 "undefined"

javascript - 如何在foreach循环内获取jquery foreach中的值

javascript - Jquery:无法在 iframe 中找到元素

javascript - 使用php中的单个按钮从计算机上传图片

javascript - useRef 用于显示文本区域内的字符数

javascript - 为什么浏览器会替换通过 css 分配的宽度值?

javascript - 使用 Jquery 将文本插入输入/文本区域

javascript - 如何使 Google 链接图表动态化