Express API 不会接受来自 POSTMAN 的任何请求

Express API wont take any requests from POSTMAN

我一直在尝试开发一个 API 用于 MEAN 堆栈应用程序。 API 处理用户注册和身份验证。 真正有趣的是它没有接受 Postman 的任何请求。即使是简单的“/”获取请求和响应也不会起作用。我在邮递员那里得到的错误如下

我的index.js如下:

const exp = require('express');
const bp = require('body-parser');
const { success, error } = require('consola')
const { connect } = require('mongoose');

// Bring in the app constants
const { DB, PORT } = require("./config");

// Initialize the application
const app = exp();

// Call the middleware
app.use(cors());
app.use(bp.json);

// User Router Middleware
app.use("/api/users", require("./routes/users"));
app.use("/basic", require("./routes/basic"));

// Connect to the database

const startApp = async () => {
    try {
      // Connection With DB
      await connect(DB, {
        useFindAndModify: true,
        useUnifiedTopology: true,
        useNewUrlParser: true
      });
  
      success({
        message: `Successfully connected with the Database \n${DB}`,
        badge: true
      });
  
      // Start Listenting for the server on PORT
      app.listen(PORT, () =>
        success({ message: `Server started on PORT ${PORT}`, badge: true })
      );
    } catch (err) {
      error({
        message: `Unable to connect with Database \n${err}`,
        badge: true
      });
      startApp();
    }
  };



startApp();

我的基本路由如下,它本身对我来说都不行:


router.get('/', function (req, res) {
    res.send('Hello World!' + req.body)
  })

module.exports = router;

邮递员回复如下图所示:

服务器确实 运行 但似乎只是拒绝了所有请求。

mongodb://localhost:27017/node-auth


 SUCCESS  Server started on PORT 3000                                                 16:26:36  

您的基本路由器是否导入快递?
它应该看起来像这样:

 const express = require("express");
 const router = express.Router();
  
 router.get("/", (req, res) => {
   res.send(`Hello World! ${req.body}`);
 });
          
 module.exports = router;

您没有使用 index.js

中同一个 express 实例的 router

抱歉。

我缺少一个 () 大括号,它允许应用程序 运行 但不正确。有时只需要另一只眼睛。

之前: app.use(bp.json); 之后:app.use(bp.json());

现在工作完美。