Angular 2 Server Express ERR_CONNECTION_REFUSED

时间:2017-06-27 15:15:44

标签: node.js angular express npm server

我创建了一个网站,使用Angular2作为前端,Node / Express作为后端。前端通过端口4200(npm运行构建以运行它)和后端通过端口4201(npm start)。

我有一个管理用户连接(登录/密码)的视图,它在localhost上完美运行。基本上,我有一个服务,将在表单提交上调用以获取指定的用户。问题是,当我在自己的服务器上尝试相同的操作时,我收到此错误:

选项http://[HostIP]:4201/auth/login

  

净:: ERR_CONNECTION_REFUSED

我的服务器位于debian7环境中的1& 1云端。防火墙似乎很清楚,端口似乎已打开。

你知道发生了什么吗?问题来自我的代码还是来自服务器?

Server.js:

const express = require('express');
const app= express();
const bodyParser= require('body-parser');

let data = require('./dataPics');

const fakeUser = {dataFakeUser};
const secret = 'mySecret';
const jwt = require('jsonwebtoken');

app.use(bodyParser.json());

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'content-type');
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  next();
});

const auth = express.Router();

auth.post('/login', (req, res) => {
  if(req.body){
    const login = req.body.login.toLocaleLowerCase();
    const password = req.body.password.toLocaleLowerCase();
    if(login === fakeUser.login && password === fakeUser.password){
      delete req.body.password;
      //res.json({ success: true, data: req.body});
      const token = jwt.sign({iss: 'http://localhost:4201', role: 'admin', user: req.body.login}, secret);
      res.json({ success: true, token: token});
    }
    else{
      res.json({ success: false, message: 'Identification Incorrecte'});
    }
  }
  else{
    res.json({ success: false, message: 'Merci de vous identifier'});
  }
});

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

const port = 4201;

app.listen(port, '[HostIP]', () => {
  console.log(`listening on port ${port}`);
});

Service.ts:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import * as jwtDecode from 'jwt-decode';

@Injectable()
export class LoginService {
// BASE_URL = 'http://localhost:4201/auth';
  BASE_URL = 'http://[HostIP]:4201/auth';

  constructor(private _http: Http) { }

  login(formData){
    return this._http.post(`${this.BASE_URL}/login`, formData)
      .map(res => res.json());
  }

  userIsLoggedIn(){
    return localStorage.getItem('jjw-data');
  }

  logOut(){
    localStorage.removeItem('jjw-data');
  }

  decodeToken(token){
    return jwtDecode(token);
  }

}

谢谢: - )

1 个答案:

答案 0 :(得分:0)

localhost是一个特殊的主机名,只有在您托管的同一台计算机网络应用程序中打开网址时才会运行,可以尝试使用您托管节点应用程序的计算机的公共IP更改http://localhost:4201 http://[publicIp]:4201

if(req.body){
        const login = req.body.login.toLocaleLowerCase();
        const password = req.body.password.toLocaleLowerCase();
        if(login === fakeUser.login && password === fakeUser.password){
          delete req.body.password;
          //res.json({ success: true, data: req.body});
          const token = jwt.sign({iss: 'http://localhost:4201', role: 'admin', user: req.body.login}, secret);
          res.json({ success: true, token: token});
        }

如果该解决方案不适合您,您可以尝试运行

  

netstat -antu

为了知道该端口是否被不同的应用程序使用。