当我试图从我的数据库中获取数据时,我得到一个信息“无法获取/api/auth/logowanie”。我正在使用node.js和mongodb。我试图解决它,但没有任何效果。(端口和URL是100%正确的)
我的auth.js文件
const router = require('express').Router();
const CryptoJs = require('crypto-js');
const User = require('../models/user');
//REJESTRACJA
router.post('/rejestracja', async (req, res)=>{
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: CryptoJs.AES.encrypt(req.body.password, process.env.PASS_SEC).toString(),
});
try{
const saveduser = await newUser.save();
res.status(201).json(saveduser);
}catch(err){
res.status(500).json(err);
}
});
//LOGOWANIE
router.post('/logowanie', async (req, res) => {
try{
const user = await User.findOne({username: req.body.username});
!user && res.status(401).json("Wrong username or password")
const hashedpassword = CryptoJs.AES.decrypt(user.password, process.env.PASS_SEC);
const password = hashedpassword.toString(CryptoJs.enc.Utf8);
password !== req.body.password &&
res.status(401).json("Wrong username or password")
res.status(200).json(user);
}catch(err){ res.status(500).json(err); }
})
module.exports = router
我的index.js文件
const express = require('express');
const app = express();
const dotenv = require('dotenv');
dotenv.config();
const userRoute = require("./routes/user");
const authRoute = require("./routes/auth");
const mongoose = require('mongoose');
const { CLIENT_RENEG_LIMIT } = require('tls');
const { application } = require('express');
mongoose
.connect(process.env.MONGO_URL)
.then(() => console.log('Connected to Mongoose server'))
.catch((err) => {
console.log(err);
});
app.use(express.json());
app.use('/api/users', userRoute);
app.use('/api/auth', authRoute);
app.listen(process.env.PORT || 5300, ()=>{
console.log('Listening on port 5300');
});
1条答案
按热度按时间cig3rfwq1#
auth.js文件中的端点是一个POST请求,而您正在从Postman发送一个GET请求。