【问题标题】:ClientSide code and server side code proplem客户端代码和服务器端代码问题
【发布时间】:2022-01-11 16:38:13
【问题描述】:

我有一个问题,要制作两个 js 文件,一个放在“网站”目录中,另一个放在外面很多,它没有工作所以.. 这是我的服务器端代码

/* Empty JS object to act as endpoint for all routes */
projectData = {};

/* Express to run server and routes */
const express = require('express');

/* Start up an instance of app */
const app = express();

/* Dependencies */
const bodyParser = require('body-parser')
/* Middleware*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const cors = require('cors');
app.use(cors());

/* Initialize the main project folder*/
app.use(express.static('website'));

const port = 3000;
/* Spin up the server*/
const server = app.listen(port, listening);
 function listening(){
    // console.log(server);
    console.log(`running on localhost: ${port}`);
  };

// GET route
app.post('/add', function (req, res) {
    let data = req.body;
    console.log(data);
});


// POST an animal
const data = []

app.post('/animal', addAnimal)

function addAnimal (req,res){
    data.push(req.body);
    console.log(data);
}


这就是我的客户端代码

/* Function to POST data */
const postData = async ( url = '', data = {})=>{
    console.log(data)
      const response = await fetch(url, {
      method: 'POST', // *GET, POST, PUT, DELETE, etc.
      credentials: 'same-origin', // include, *same-origin, omit
      headers: {
          'Content-Type': 'application/json',
      },
      body: JSON.stringify(data), // body data type must match "Content-Type" header        
    });
  
      try {
        const newData = await response.json();
        console.log(newData);
        return newData
      }catch(error) {
      console.log("error", error);
      // appropriately handle the error
      }
  }
  
  // TODO-Call Function
  postData('/addAnimal', {animal:'Tiger'});
  postData('/addAnimal',  {animal:'Lion'});

当我在 vs 代码编辑器中运行代码时,它会显示“{ animal: 'lion' } { 动物:'老虎'}"

但它从不控制台记录数据

【问题讨论】:

    标签: javascript node.js server server-side-rendering


    【解决方案1】:

    您忘记发送回复 路由回调必须有 res 端点

    function addAnimal (req,res){
        data.push(req.body);
        console.log(data);
        // add res end point 
        res.json({msg : 'added'})
    }
    //here too
    app.post('/add', function (req, res) {
        let data = req.body;
        console.log(data);
        res.end('hello world')
    });
    

    【讨论】:

    • 我也试过了,没用
    【解决方案2】:

    您的路线是 /add 或 /animal 而不是 /addAnimal

    postData('/add', {animal:'Tiger'});
    

    在你的服务器端这个函数应该显示一个日志

    app.post('/add', function (req, res) {
        let data = req.body;
        console.log(data);
    });
    

    您不能 console.log 在您的 try / catch 中记录数据,因为您没有在服务器端返回任何响应。但首先尝试在服务器端控制器中记录数据以确认服务器端执行良好,然后返回您的响应。

    【讨论】:

    • 好的,我试过了,但它没有控制台。记录它@Mino mnz
    • ok itried 这个但它没有 console.log 它虽然
    • 好的,您的后端控制台中没有跟踪: postData('/animal', {animal:'Tiger'}); ?
    • 痕迹是什么?
    • 控制台日志
    猜你喜欢
    • 2011-07-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多