Javascript 从获取路由(nodejs)检索数据
我正在从一个网站学习nodejs和数据库 在本网站中,为了从app.use('/server/profil')检索数据,我们必须复制以下代码 我是这样做的,但我有一个问题: app.post中的req.body检索空对象。它应该检索stud对象 需要帮忙吗 谢谢你能帮助我的人Javascript 从获取路由(nodejs)检索数据,javascript,node.js,express,post,get,Javascript,Node.js,Express,Post,Get,我正在从一个网站学习nodejs和数据库 在本网站中,为了从app.use('/server/profil')检索数据,我们必须复制以下代码 我是这样做的,但我有一个问题: app.post中的req.body检索空对象。它应该检索stud对象 需要帮忙吗 谢谢你能帮助我的人 const express = require('express'); const app = express() const cors = require('cors'); app.us
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;