无法发布到路由

时间:2021-05-25 02:24:02

标签: node.js api

我正在 node js 中实现一个简单的注册/登录功能 但是当使用邮递员将路由器发布到 localhost:3000/api/chatapp/register 时,我收到了错误消息 无法 POST /api/register

代码:

server.js

const express=require('express');
const mongoose=require('mongoose');
const cookiePaser=require('cookie-parser');
// const logger=require('morgan');

const app=express();

const dbConfig= require('./config/secret');

//adding middleware
app.use(cookiePaser()); //save our token in the cookie
// app.use(logger('dev')); // to display the url and http status code in the console

mongoose.Promise = global.Promise;
mongoose.connect(
    dbConfig.url,{useNewUrlParser: true, useUnifiedTopology: true}
    );

const auth=require('./routes/authRoutes');

app.use(express.json({limit: '50mb'})); //data coming in from form is limited up to 50 mb size
app.use(express.urlencoded({ extended: true, limit:'50mb'}));
app.use('/api/chatapp',auth);

app.listen(3000, () => {
    console.log('Running on port 3000');
})

authRoute.js

const express=require('express');
const router=express.Router();

const AuthCtrl=require('../controllers/auth');

router.post('/register',AuthCtrl.CreateUser);

module.exports=router;

auth.js

module.exports={
  CreateUser(req,res){
    console.log(req.body);
  }
};

userSchema.js

const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
  username: { type: String },
  email: { type: String },
  password: { type: String },
});

module.exports=mongoose.model('User',userSchema);

nodemon 服务器和 mongod 运行良好

Postman result

有关更多详细信息,请查看我的 github 存储库: https://github.com/Umang01-hash/chatApp-backend

1 个答案:

答案 0 :(得分:0)

您确定那是正确的端点吗?查看文件夹结构并查看此图片以获得答案。enter image description here

相关问题