无法在节点js中进行身份验证

时间:2018-12-27 06:34:32

标签: node.js angular authentication mean-stack

我正在尝试从Udemy构建此应用。我知道后端通过邮递员和mongo指南针进行测试可以正常工作。修复了几段代码后,我不断收到这些未知错误。我不知道什么是行不通的。我尝试删除core res标头请求,并添加了它们仍然无法到达任何地方。

signup.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';

@Component({
 selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit {
  signupForm: FormGroup;
  errorMessage: string;
  showSpinner = false;

  constructor(private authService: AuthService, private fb: FormBuilder, private router: Router) { }

  ngOnInit() {
    this.init();
  }

  init() {
    this.signupForm = this.fb.group({
      username: ['', Validators.required],
      email: ['', [Validators.email, Validators.required]],
      password: ['', Validators.required]
    });
  }

  signupUser() {
    this.showSpinner = true;
    console.log(this.signupForm.value);
    this.authService.registerUser(this.signupForm.value).subscribe(
      data => {
      console.log(data);
      this.signupForm.reset();
      setTimeout(() => {
        this.router.navigate(['streams']);
      }, 2000);
    },
     err => {
       this.showSpinner = false;
       if (err.error.msg) {
         this.errorMessage = err.error.msg[0].message;
       }

       if(err.error.message) {
         this.errorMessage = err.error.message;
       }
     }
  );
 }
}

auth.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

const BASEURL = 'http://localhost/3000/api/chatapp';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  constructor(private http: HttpClient) { }

  registerUser(body): Observable<any> {
    return this.http.post<any>(`${BASEURL}/register`, body);
  }
}

server.js

const express = require('express');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const cors = require('cors');
// const logger = require('morgan');
const bodyParser = require('body-parser');

const app = express();

app.use(cors());

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

app.use((req, res, next)  => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Credentials", "true");
    res.header('Access-Control-Allow-Methods', 'GET', 'POST', 'DELETE', 'PUT');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({extended: true, limit: '50mb'}));
app.use(cookieParser());
// app.use(logger('dev'));

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

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

app.use('/api/chatapp', auth);

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

结果:

zone.js:2969 OPTIONS http://localhost/3000/api/chatapp/register net::ERR_CONNECTION_REFUSED

0 个答案:

没有答案