post Firefox 中的端点 404 和 chrome 中的待定

post endpoint 404 in firefox and pending in chrome

按照此快速指南 (React and PostgreSQL),以下应用应将 JSON 提取打印到 bash 终端(在视频的 ~37 分钟处)。

然而这并没有发生。 npm 或 nodemon 服务器上没有任何反馈。

通过前端添加值时,firefox 会立即发回 404 状态(在 console:network 中观察到)。在 chrome 中,线程挂起,直到 nodemon 服务器关闭(然后因连接重置错误而失败)(再次在 console:network 中)。

npm 是 运行 应用程序,nodemon 是 运行 服务器。

app.js

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

class App extends Component {
  constructor(props) {
        super();  
        this.state = {
          title: 'Simple postgres app',
          treatments: []
        }
  }

  componentDidMount(){
        console.log('COMPONENT HAS MOUNTED')
  }   

  addStuff(event){
    event.preventDefault()
    // console.log('in method');

    let data = {
            test_field: this.refs.test_field.value,
    };

    var request = new Request('http://localhost:3000/api/new-thing', {
            method: 'POST', 
            headers: new Headers({ 'Content-Type': 'application/json' }),
            body: JSON.stringify(data), 
            message: console.log('JSON output: ', JSON.stringify(data))
    }); 

    fetch(request)
      .then((response) => {
        response.json()
          .then((data) => {
            console.log(data)
          })
      })
  }

  render() {
    let title = this.state.title;
    return (
      <div className="App">
        <h1> { title } </h1>  
        <form ref = "testForm">
                        <input type="text" ref="test_field" placeholder="test_field"/> 
                        <button onClick={this.addStuff.bind(this)}>Add This</button>
                </form>
      </div>
    );
  }
}

export default App;

server.js

let express = require('express');
let bodyParser = require('body-parser');
let morgan = require('morgan');
let pg = require('pg');
const PORT = 3000;

// let pool = new pg.Pool({
//         port: 5432,
//         user: 'postgres',
//         password: 'postgres',
//         database: 'po1dev_v0.0.1',
//         max: 10, //max connections
//         host: 'localhost'
// })

let app = express();

app.use(bodyParser.json);
app.use(bodyParser.urlencoded({ extended:true }));

app.use(morgan('dev')); 

app.use((request, response, next) => {
        response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
});   

// app.post('/api/new-thing', (request,response) => {
//     console.log(request.body)
// })
app.post('/api/new-thing', function(request,response){
    console.log(request.body);
})

app.listen(PORT, () => console.log('Listening on port ' + PORT));

关于什么可能导致 firefox/chrome 中的 404/挂起问题以及如何解决它的任何想法?

干杯

那是因为您正在创建的路由 return 没有任何响应,因此它会无限期地等待响应然后超时。

路由应该return一些响应,

app.post('/api/new-thing', function(request,response){
    console.log(request.body);
    return response.json({"data": "Hello World"})
})

return 来自 /api/new-thing 路线的 {"data": "Hello World"}

此外,bodyParser.json 不是 属性 的函数。将其更改为

app.use(bodyParser.json())

如果您正在使用 create-react-app,请为后端服务器尝试另一个端口。因为默认情况下它使用 3000 作为 React 应用程序。