GET http://localhost:3000/hello 404(未找到)

阿布迪-悉尼

我正在遵循實施 MERN 的教程,但我遇到了以下錯誤:GET http://localhost:3000/hello 404 (Not Found)我做的一切都是正確的,但我無法解決這個問題。以下是我的文件,任何人都可以形成社區幫助,以便我繼續前進。我真的很感謝你的幫助。謝謝你們。

前端文件夾:App.js

import React, { Component} from 'react' ;
// import logo from './logo.svg';
import './App.css';
import axios from 'axios' ;

class App extends Component {
   state = {
     hello: null
   }

  componentDidMount() {
      axios.get('/hello')
      .then( res => this.setState({ hello: res.data }))
      .catch( err => console.log(err) )
  
  }

  render() {
     return (
        <div>
          {this.state.hello
            ? <div> {this.state.hello} </div>
            : null }
        </div>
      );
  }
}

export default App;

後端文件夾:App.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');


//import the 'routes' file
var indexRouter = require('./routes')


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


// use indexRouter
app.use('/', indexRouter);

module.exports = app;

路線

var express = require('express');

var router = express.Router()  

router.get('/hello', function(req, res) {
    res.json('hello world') ;
})

// this is exported to app.js in the Server folder
module.exports =  router ;

前端文件夾:package.json

...
"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "proxy": "http://localhost:5000", <==== pointing to localhost
  "browserslist": {
    "production": [
      ">0.2%",
 ...

斯特凡·日夫科維奇

好的,我拿走了你的代碼,第一個問題是你的請求被發送到http://localhost:3001/hello- 這意味著它將佔用你前端的端口(假設你想分別運行 FE 和 BE,例如端口 3000 和 3001)在此處輸入圖片說明

在我修復axios.get('/hello')axios.get('http://localhost:3000/hello'),我們進入第二個問題。我收到 CORS 問題。在此處輸入圖片說明

為了解決這個問題,我們必須cors在我們的快速後端進行配置- https://expressjs.com/en/resources/middleware/cors.html操作:npm i cors並添加app.use(cors());Now 如果我們重新運行後端node index.js並刷新前端頁面 - 我們會得到響應 200 OK。在此處輸入圖片說明

第三個問題是 - 你沒有app.listen().app.listen(PORT, () => console.log(`app listening at http://localhost:${PORT}`))

好的,這是代碼。首先,我刪除了所有我認為與這種情況無關的額外依賴項。然後在我讓它全部工作後,我把它們放回去,但在某些地方我可能忘記全部歸還。此外,我將您的get路線直接放入index.js- 因此它也與您的代碼不同(您將其放入單獨的模塊中)。正如我所說,我盡量讓事情簡單易懂。

BE(我用node index.js

var express = require('express');
var path = require('path');
var cors = require('cors')
var createError = require('http-errors');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var app = express();
const PORT = 3000;

app.use(cors())
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'));
app.use(cookieParser());
 
app.get('/hello', function(req, res) {
    res.send('hello world') ;
})
app.listen(PORT, () => console.log(`app listening at http://localhost:${PORT}`))

module.exports = app;

FE(以npm start- 它是 create-react-app - 端口 3001 開始,因為後端在端口 3000 上運行)

export  class App extends React.Component {
  state = {
    hello: null
  }
 componentDidMount() {
     axios.get('http://localhost:3000/hello')
     .then( res => this.setState({ hello: res.data }))
     .catch( err => console.log(err) )
 
 }
 render() {
    return (
       <div>
         {this.state.hello
           ? <div> {this.state.hello} </div>
           : null }
       </div>
     );
   }
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

GET http://localhost:3000/:/localhost:4000 404(未找到)

POST http:// localhost:3000/404(未找到)

GET http:// localhost:3000/404(未找到),实际上是在拨打POST却获得GET

GET http:// localhost:3000 / components / t.mp3 net :: ERR_ABORTED 404(未找到)

Angular 2 + angular-in-memory-web-api区域GET http:// localhost:3000 / traceur 404(未找到)

我收到错误 GET http://localhost:3000/api/products 404(未找到)

识别:9 GET http://localhost:3000/script.js net::ERR_ABORTED 404(未找到)

PUT http://localhost:3000/events/undefined 404(未找到)

GET http://localhost:5000/auth/me 404(未找到)

GET http:// localhost:4200 / resource 404(未找到)

GET http://localhost:5000/error 404 (未找到)

http:// localhost / undefined 404(未找到)

XHR错误(404未找到)正在加载http:// localhost:3000 / traceur

在angular4中获取http:// localhost:3000 / node_modules / rxjs / 404(未找到)

<未定义:1 GET http:// localhost:63342 / Twitchtv /未定义404(未找到)>

ionic3 GET http:// localhost:8100 / null 404(未找到)

离子2 GET http:// localhost:8100 / search / aq?query = 404(未找到)

GET http://localhost:4200/src/app/ficheros/nacionalidades.json 404(未找到)

使用 Pug 表达:GET http://localhost:3004/contact 404(未找到)

Laravel - 依赖下拉错误:GET http://localhost:8888/get.employee.states?country_id=5 404(未找到)

GET http:// localhost:4200 / node_modules / zone.js / dist / zone.js 404(未找到)

GET http:// localhost:8080 / socket.io /?EIO = 3&transport = polling&t = McNiz_D 404(未找到)

Laravel Jquery Dependent下拉列表显示错误GET http:// localhost:8000 / dosen / pengajuan / getKK / 4 404(未找到)

Laravel 9 - GET http://localhost:8000/css/app.css net::ERR_ABORTED 404(未找到)

POST http:// localhost:4200 / contact / send 404(未找到)

POST http://localhost:55703/ValidStep1 404(未找到)

未找到 Http 404

“http://localhost:49540/api/create/ 的 HTTP 失败响应:404 未找到”Angular 11 POST 请求

(404未找到)正在加载http:// localhost:4200 / angular2-datatable / datatable