'Cannot POST /api/auth/registration - problem with server
So, I learn backend, and make a cloud app. I want to explore my server on registration. It`s work on POST only, but there is already bugs. I try Postman to check request, and I get a mistake there:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/auth/registration</pre>
</body>
</html>
my code: index.js:
const express = require('express');
const config = require('config');
const mongoose = require('mongoose');
const authRouter = require('./routes/auth.routes')
const app = express();
const PORT = config.get('serverPort');
app.use('api/auth', authRouter)
const start = async() => {
try{
await mongoose.connect(config.get('DbUrl'))
app.listen(PORT, () => {
console.log('listening on port', PORT)
})
} catch(e){
console.log(e)
}
}
start()
auth.routes.js:
const Router = require('express')
const User = require('../module/User')
const bcrypt = require('bcryptjs')
const {check, validationResult} = require('express-validator')
const routes = new Router();
routes.post('/registration',
[
check('email', 'Incorrect email').isEmail(),
check('password', 'must be longer than 3 and shorter than 13 symb').isLength({min: 3, max: 13})
],
async (res, req) => {
try{
const errors = validationResult(req)
if(!errors.isEmpty()){
return res.status(400).json({message: 'Incorrect request', errors})
}
const {email, password} = res.body;
const candidate = User.findOne({email})
if(candidate){
return res.status(400).json({message: 'User already registered'})
}
const hashPassword = await bcrypt.hash(password, 15)
const user = new User({email, password: hashPassword})
await user.save()
return res.status(200).json({message: 'User was created successfully'})
} catch (e){
}
})
module.exports = routes
User.js:
const {model, Schema} = require("mongoose");
const User = new Schema({
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
diskSpace: {type: Number, default: 1024**3*10},
usedSpace: {type: Number, default: 0},
avatar: {type: String},
files: [{type: Object, ref: 'File'}]
})
module.exports = model('User', User)
I try to do smth, but nothing isn't help.Could u help me with that? I don`t understand, what I have to do
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
