gpt4 book ai didi

javascript - 无法获取/帖子

转载 作者:行者123 更新时间:2023-12-03 12:14:08 27 4
gpt4 key购买 nike

我试图访问我的路由“posts.js”,但是当我启动服务器并连接到 localhost:5000/posts 时。此错误显示为“无法获取/发布”
代码:服务器/路由/posts.js

import express from 'express';

import { getPost, createPost } from '../controller/posts.js'

const router = express.Router();

router.get('/', getPost);
router.post('/', createPost);

export default router;

代码:服务器/模型/postMessages.js
import mongoose from 'mongoose';

const postSchema = mongoose.Schema({
title: String,
message: String,
creator: String,
tags: [String],
selectedFile: String,
likeCount: {
type: Number,
default: 0
},
createdAt: {
type: Date,
default: new Date()
},
});

const PostMessage = mongoose.model('PostMessage', postSchema);

export default PostMessage;
代码:服务器/ Controller /posts.js
import PostMessage from '../models/postMessages.js'



export const getPost = async (req, res) => {
try {
const postMessages = await PostMessage.find();

res.status(200).json(postMessages);
} catch(error) {

res.status(404).json({ message: error.message });

}
};

export const createPost = (req, res) => {
res.send('Posts creation');
};
当我启动服务器时,在终端显示这个
[nodemon] starting `node index.js`
(node:12344) ExperimentalWarning: The ESM module loader is experimental.
Server running on PORT 5000:

最佳答案

您需要更改 router.get('/', getPost);router.get('/posts', getPost);因为现在它响应'/',但你希望它响应'/posts'

关于javascript - 无法获取/帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65417985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com