首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获取“/tinder/card”路径,但获取了"/“路径

无法获取“/tinder/card”路径,但获取了"/“路径
EN

Stack Overflow用户
提问于 2020-10-28 00:04:02
回答 2查看 122关注 0票数 0

下面的代码是我的第一个react应用程序。在运行server.js之后。在输入"http://localhost:8001/“后,我得到了HELLO!!。我预计在chrome和postman上输入"http://localhost:8001/tinder/cards“url后,我也会得到以下错误。

错误消息:"Cannot GET /tinder/card“。

这是我的server.js文件。

代码语言:javascript
复制
import mongoose from 'mongoose'
import Cors from 'cors'
import Cards from './dbCards.js'


// App Config
const app = express()
const port = process.env.PORT || 8001
const connection_url = 'mongodb+srv://admin:0tRkopC1DKm4ym4V@cluster0.iw73w.mongodb.net/tinderDB?retryWrites=true&w=majority'

// Middlewares
app.use(express.json())
app.use(Cors())
app.use('/',router);

// DB Config
mongoose.connect(connection_url, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
})

// API Endpoints
app.get("/", (req, res) => res.status(200).send("HELLO!!"))
app.get("/hello", (req, res) => res.status(200).send("Oooooooo!!"))

app.post("/tinder/cards", (req, res) => {
    const dbCard = req.body
    Cards.create(dbCard, (err, data) => {
        if(err) {
            res.status(500).send(err)
        } else {
            res.status(201).send(data)
        }
    })
})

app.get("/tinder/cards", (req, res) => {
    Cards.find((err, data) => {
        if(err) {
            res.status(500).send(err)
        } else {
            res.status(200).send(data)
        }
    })
})

// Listener
app.listen(port, () => console.log(`listening on location: ${port}`)) ```
EN

回答 2

Stack Overflow用户

发布于 2020-10-28 01:34:19

在你的app.get('/tinder/cards')部分试试这个:

代码语言:javascript
复制
// Doing it the Asynchronous way

app.get('/tinder/cards', async (req, res) => {
  try { // Trying for getting the cards
    const allCards = await Cards.find(); // Getting the cards
    res.status(200).send(allCards); // Sending the cards
  } catch (error) { // Catching the error
    res.status(500).send(error); // Sending the error
  }
});
票数 0
EN

Stack Overflow用户

发布于 2021-09-26 13:39:20

尝试将URL中的cards替换为cardHave a look at the image for the same.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64558360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档