微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从获取路由nodejs中检索数据

如何解决从获取路由nodejs中检索数据

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

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

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

需要帮助

感谢那些能帮助我的人。

    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,next) => {
    console.log(req.body);
    res.status(201).json({
    message: 'Thing created successfully!'
    });
    });

    app.use('/server/profil',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',price: 2900,];
    res.status(200).json(stuff);
    res.send(stuff);
    });

    module.exports = app;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。