Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/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
Node.js React Project-对ExpressJS服务器的请求返回400-请求错误_Node.js_Sql Server_Reactjs_Express_Tedious - Fatal编程技术网

Node.js React Project-对ExpressJS服务器的请求返回400-请求错误

Node.js React Project-对ExpressJS服务器的请求返回400-请求错误,node.js,sql-server,reactjs,express,tedious,Node.js,Sql Server,Reactjs,Express,Tedious,设置参数,按下按钮后,expressjs请求将接收请求[Node request package]或获取请求。创建链接地址时,返回的响应是400个错误请求 我尝试了一个获取请求和一个节点包'request',并且 服务器JS const express = require ('express'); const path = require('path') //core node module const app = express(); const cors =

设置参数,按下按钮后,expressjs请求将接收请求[Node request package]或获取请求。创建链接地址时,返回的响应是400个错误请求

我尝试了一个获取请求和一个节点包'request',并且

服务器JS

    const express = require ('express');
    const path = require('path') //core node module
    const app = express();
    const cors = require('cors');
    const router = express.Router();

    // app.use(cors())
    const publicdirpath = path.join(__dirname, '../public')
    console.log(path.join(__dirname, '../public'))

    app.use(cors());
    app.use(express.static(publicdirpath))

    app.post('/testcall', (req, res) => {
        if(!req.query.startdate && !req.body.enddate && 
    !req.body.projectnumber){
        return res.status(400).send({
            success: 'false',
            message: 'dates or project or both required'
        });
     }

     //call stored procedures
   });

    const port = process.env.PORT || 3000;
    app.listen(port, () => {
    console.log(`Server running on port ${port}`);
   });

    handleDropdownClick = (event, selection) =>{
        const { name, innerText, value } = event.target;
        console.log( event.target  + " : " + innerText + " : " + 
    this.props.formData);
        const request = require('request');
        switch(selection){
            case 1:
                //call api or stored procedure 
                if(this.validation()){
                    //call api
                 request.get({url:"http://localhost:3000/testcall", qs:this.state.formData} , function (err, res, body) {
                        if(err){
                            console.error('error with request: error: ' + err + '. res: ' + res +  ' + body: ' + body);
                        }
                        console.log("Get response: " + res.statusCode + ". Body: " + body);

                    })

                    //Using Fetch
                    const jsonData = JSON.stringify(this.state.formData);
                    fetch('/testcall', {
                        method: "POST",
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/json',
                            'credentials': 'include'
                        },
                        body: jsonData
                    })
                    .then( response => { 
                        return response.json();
                    })
                    .then( response => {
                        console.log(response);
                    })
                    .catch(function (e) {
                        console.log("fail: " + e);
                    })
                }
                break;
    //more code
调用express JS的JS类

    const express = require ('express');
    const path = require('path') //core node module
    const app = express();
    const cors = require('cors');
    const router = express.Router();

    // app.use(cors())
    const publicdirpath = path.join(__dirname, '../public')
    console.log(path.join(__dirname, '../public'))

    app.use(cors());
    app.use(express.static(publicdirpath))

    app.post('/testcall', (req, res) => {
        if(!req.query.startdate && !req.body.enddate && 
    !req.body.projectnumber){
        return res.status(400).send({
            success: 'false',
            message: 'dates or project or both required'
        });
     }

     //call stored procedures
   });

    const port = process.env.PORT || 3000;
    app.listen(port, () => {
    console.log(`Server running on port ${port}`);
   });

    handleDropdownClick = (event, selection) =>{
        const { name, innerText, value } = event.target;
        console.log( event.target  + " : " + innerText + " : " + 
    this.props.formData);
        const request = require('request');
        switch(selection){
            case 1:
                //call api or stored procedure 
                if(this.validation()){
                    //call api
                 request.get({url:"http://localhost:3000/testcall", qs:this.state.formData} , function (err, res, body) {
                        if(err){
                            console.error('error with request: error: ' + err + '. res: ' + res +  ' + body: ' + body);
                        }
                        console.log("Get response: " + res.statusCode + ". Body: " + body);

                    })

                    //Using Fetch
                    const jsonData = JSON.stringify(this.state.formData);
                    fetch('/testcall', {
                        method: "POST",
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/json',
                            'credentials': 'include'
                        },
                        body: jsonData
                    })
                    .then( response => { 
                        return response.json();
                    })
                    .then( response => {
                        console.log(response);
                    })
                    .catch(function (e) {
                        console.log("fail: " + e);
                    })
                }
                break;
    //more code
Package.json

    {
      "name": "app-name",
      "version": "0.1.0",
      "private": true,
      "main": "index.js",
      "dependencies": {
      "body-parser": "^1.19.0",
      "cors": "^2.8.5",
      "express": "^4.17.1",
      "nodemon": "^1.19.1",
      "react": "^16.8.6",
      "react-dom": "^16.8.6",
      "react-scripts": "3.0.1",
      "request": "^2.88.0",
      "save": "^2.4.0",
      "semantic-ui-react": "^0.87.2",
      "table": "^5.4.1",
      "tedious": "^6.2.0",
      "webpack": "^4.29.6"
     },
    "scripts": {
      "start": "react-scripts start",
      "build": "react-scripts build",
      "test": "react-scripts test",
      "eject": "react-scripts eject",
      "dev": "run-p server start"
    },
    "eslintConfig": {
      "extends": "react-app"
    },`enter code here
    "browserslist": {
      "production": [
        ">0.2%",
        "not dead",
        "not op_mini all"
      ],
      "development": [
        "last 1 chrome version",
        "last 1 firefox version",
        "last 1 safari version"
      ]
    }, 
    "proxy":"http://localhost:3000"
  }
Index.js

    import React from 'react';  
    import ReactDOM from 'react-dom';
    import './index.css';
    import App from './App';
    import * as serviceWorker from './serviceWorker';


    ReactDOM.render(<App />, document.getElementById('root'));

    serviceWorker.unregister();
expressjs接受调用并完成逻辑是预期的结果

实际结果为:

400后(请求错误) -用于npm包请求

js:149 GET 400(错误请求)
-对于获取请求

您可以使用axios或在前端使用代理

我建议您也使用axios,它比获取更易于使用。你可以在这里找到更多


不幸的是,axios也得到了同样的结果。我想知道问题是否在于express is服务器文件设置错误。也许它根本没有收到请求