问题描述
我正在从网站学习 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 (将#修改为@)