Angular 4在生产模式中找不到404

时间:2017-08-16 06:11:51

标签: node.js angular http

我正在构建一个角度节点应用程序并正在进行http post请求以注册用户。它是一系列可观察信息,可以获取用户的社交登录信息,注册用户,然后成功通过电子邮件发送给用户。在开发模式下,一切都很完美,在产品模式下,我找不到404。我还想要注意,在开发模式中,我在成功的observable中的一些函数调用没有被调用。它的行为非常奇怪,无法弄清楚我做错了什么。

这是我的路线控制器

module.exports = {
  signup: function signup(req, res) {
    return User.create({
      email: (req.body.email).toLowerCase(),
      image: req.body.image,
      name: req.body.name,
      provider: req.body.provider || 'rent',
      uid: req.body.uid || null
    }).then(function (user) {
      return res.status(200).json({
        title: "User signed up successfully",
        obj: user
      });
    }).catch(function (error) {
      console.log(error);
        return res.status(400).json({
          title: 'There was an error signing up!',
          error: error
        });
      });
  }
};

和路线

router.post('/signup', function(req,res,next) {
  return usersController.signup(req,res);
});

我的服务

@Injectable()
export class UserService {

  private devUrl = 'http://localhost:3000/user';
  private url = '/user';
  sub: any;
  public user: User;

  constructor(
    private authS: AuthService,
    private http: HttpClient) {


  }

  signup(user: User) {
    return this.http.post(this.url + '/signup', user);
  }

  auth(provider: string) {
    return this.sub = this.authS.login(provider);
  }

  logout() {
    this.authS.logout()
      .subscribe(value => {
        console.log(value);
      }, error => console.log(error))
  }

  getUser() {
    return this.user;
  }

}

和我使用社交按钮注册的组件逻辑

  onAuth(provider: string){
      this.checkmark = true;
      this.uis.onSignupComplete();
      setTimeout(() => {
        this.checkmark = false;
        this.thankyou = true;
      }, 2500);
    this.userService.auth(provider)
      .subscribe(user => {
        this.user = {
          email: user['email'],
          image: user['image'],
          name: user['name'],
          provider: user['provider'],
          uid: user['uid']
        };
        this.userService.signup(this.user)
          .subscribe(user => {
            this.randomNum = Math.floor(Math.random() * 2);
            let userEmail = this.user.email;
            let subject = 'Welcome to the rent community';
            let html = '';
            if (this.randomNum === 1) {
              html = this.contactS.getAutoEmail1();
            } else if (this.randomNum === 0) {
              html = this.contactS.getAutoEmail0();
            }
            let email = new Email(subject, html, userEmail);
            this.contactS.setEmail(email)
              .subscribe(data => {
            }, response => {
              // if (response.error['error'].errors[0].message === 'email must be unique') {
              //   this.uis.onSetError('Email is already used');
              //   this.uis.onError();
              //   setTimeout(() => {
              //     this.uis.onErrorOff();
              //   }, 2500);
              // } else {
              //   this.uis.onSetError('There was an error');
              //   this.uis.onError();
              //   setTimeout(() => {
              //     this.uis.onErrorOff();
              //   }, 2500);
              // }
              console.log(response);
            });
          }, resp => console.log(resp));
      }, response => {

        console.log(response);
      });
  }

这是整个组件

import {Component, DoCheck, HostListener, OnInit} from '@angular/core';
import {User} from "../user/user.model";
import {UserService} from "../user/user.service";
import {UiService} from "../ui.service";
import {ContactService} from "../contact/contact.service";
import {Email} from "../contact/email.model";

@Component({
  selector: 'app-landing-page',
  templateUrl: './landing-page.component.html',
  styleUrls: ['./landing-page.component.css']
})
export class LandingPageComponent implements OnInit, DoCheck {
  user: User = {
    email: '',
    image: '',
    name: '',
    provider: '',
    uid: ''
  };
  signupComplete = false;
  signup = false;
  contact = false;
  error = false;
  errorStr = 'There was an error';
  checkmark = false;
  thankyou = false;
  randomNum = Math.floor(Math.random() * 2);


  @HostListener('window:keyup', ['$event'])
  keyEvent(event: KeyboardEvent) {
    const enter = 13;
    if (event.keyCode === enter) {
      // this.onSignup();
    }
  }

  constructor(
    private uis: UiService,
    private contactS: ContactService,
    private userService: UserService) { }

  ngOnInit() {}

  ngDoCheck() {
    this.signup = this.uis.onGetSignup();
    this.contact = this.uis.onGetContact();
    this.signupComplete = this.uis.onReturnSignupComplete();
    this.error = this.uis.getError();
    this.errorStr = this.uis.getErrorStr();
  }

  onClickAction(s: string) {
    this.uis.onClickAction(s);
  }

  onSignup() {
    this.user.provider = 'rent';
    this.user.uid = null;
    this.userService.signup(this.user)
      .subscribe(user => {
        this.randomNum = Math.floor(Math.random() * 2);
        let userEmail = this.user.email;
        let subject = 'Welcome to the rent community';
        let html = '';
        if (this.randomNum === 1) {
          html = this.contactS.getAutoEmail1();
        } else if (this.randomNum === 0) {
          html = this.contactS.getAutoEmail0();
        }
        let email = new Email(subject, html, userEmail);
        this.checkmark = true;
        this.uis.onSignupComplete();
        setTimeout(() => {
          this.checkmark = false;
          this.thankyou = true;
        }, 2500);
        this.contactS.setEmail(email)
          .subscribe(email => {
              this.onReset();
            }
          , error => console.log(error));
      }, response => {
        if (response.error['error'].errors[0].message === 'email must be unique') {
          this.uis.onSetError('Email is already used');
          this.uis.onError();
          setTimeout(() => {
            this.uis.onErrorOff();
          }, 2500);
          console.log(response);
        } else {
          this.uis.onSetError('There was an error');
          this.uis.onError();
          setTimeout(() => {
            this.uis.onErrorOff();
          }, 2500);
          console.log(response);
        }
      });
  }

  onAuth(provider: string){
      this.checkmark = true;
      this.uis.onSignupComplete();
      setTimeout(() => {
        this.checkmark = false;
        this.thankyou = true;
      }, 2500);
    this.userService.auth(provider)
      .subscribe(user => {
        this.user = {
          email: user['email'],
          image: user['image'],
          name: user['name'],
          provider: user['provider'],
          uid: user['uid']
        };
        this.userService.signup(this.user)
          .subscribe(user => {
            this.randomNum = Math.floor(Math.random() * 2);
            let userEmail = this.user.email;
            let subject = 'Welcome to the rent community';
            let html = '';
            if (this.randomNum === 1) {
              html = this.contactS.getAutoEmail1();
            } else if (this.randomNum === 0) {
              html = this.contactS.getAutoEmail0();
            }
            let email = new Email(subject, html, userEmail);
            this.contactS.setEmail(email)
              .subscribe(data => {
            }, response => {
              // if (response.error['error'].errors[0].message === 'email must be unique') {
              //   this.uis.onSetError('Email is already used');
              //   this.uis.onError();
              //   setTimeout(() => {
              //     this.uis.onErrorOff();
              //   }, 2500);
              // } else {
              //   this.uis.onSetError('There was an error');
              //   this.uis.onError();
              //   setTimeout(() => {
              //     this.uis.onErrorOff();
              //   }, 2500);
              // }
              console.log(response);
            });
          }, resp => console.log(resp));
      }, response => {

        console.log(response);
      });
  }

  onReset() {
    this.user.email = '';
    this.user.image = '';
    this.user.name = '';
    this.user.provider = '';
    this.user.uid = '';
  }

  errorStyle(): Object {
    if (this.error) {
      return {height: '50px', opacity: '1'};
    } else if (!this.error) {
      return {height: '0', opacity: '0'};
    }
    return {};
  }
}

我想提一下我正在使用angular-2-social-login进行社交登录。如果我正确使用/ user / signup路由,不确定为什么会调用404。

0 个答案:

没有答案