【问题标题】:Retrieve data from get route (nodejs)从获取路由(nodejs)中检索数据
【发布时间】:2021-04-01 16:30:05
【问题描述】:

我正在从一个网站学习 nodejs 和数据库。

在本网站中,我们必须复制以下代码才能从 app.use('/server/profil'); 中检索数据;

我是这样做的,但我有一个问题: app.post 中的 req.body 检索一个空对象。它应该检索 studd 对象。

需要帮助

感谢那些可以帮助我的人。

    const express = require('express');

    const app = express()
    const cors = require('cors');
    app.use(cors());

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

    // so for every file we want on public we do not need to specify the path as it is allready         specified.
    // indeed this code give us this path http://localhost:3000/public
    app.use(express.static('public'));

    app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept,         Content-Type, Authorization');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
    next();
    });

    app.post('/server/profil', (req, res, next) => {
    console.log(req.body);
    res.status(201).json({
    message: 'Thing created successfully!'
    });
    });

    app.use('/server/profil', (req, res, next) => {
    const stuff = [
    {
    _id: 'oeihfzeoi',
    title: 'My first thing',
    description: 'All of the info about my first thing',
    imageUrl: '',
    price: 4900,
    userId: 'qsomihvqios',
   },
   {
    _id: 'oeihfzeomoihi',
    title: 'My second thing',
    description: 'All of the info about my second thing',
    imageUrl: '',
    price: 2900,
    userId: 'qsomihvqios',
    },
    ];
    res.status(200).json(stuff);
    res.send(stuff);
    });

    module.exports = app;

【问题讨论】:

    标签: javascript node.js express post get


    【解决方案1】:

    检查 Postman 的 api,您需要使用配置您的请求。 然后您需要使用查询字符串或正文请求类型之类的东西。 (查看下图)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多