【问题标题】:Angular 2 Server Express ERR_CONNECTION_REFUSEDAngular 2 Server Express ERR_CONNECTION_REFUSED
【发布时间】:2017-11-30 17:24:15
【问题描述】:

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

我有一个视图来管理在 localhost 上完美运行的用户连接(登录名/密码)。基本上,我有一项服务将在表单提交时调用以获取指定的用户。问题是当我在自己的服务器上尝试同样的事情时,我得到了这个错误:

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

net::ERR_CONNECTION_REFUSED

我的服务器位于 1&1 云上,在 debian7 环境中。防火墙似乎畅通无阻,端口似乎已打开。

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

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);
  }

}

谢谢:-)

【问题讨论】:

    标签: node.js angular express npm server


    【解决方案1】:

    localhost 是一个特殊的主机名,仅当从托管的同一台机器 Web 应用程序打开 url 时才会运行,您可以尝试使用托管节点应用程序的机器的公共 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

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

    【讨论】:

    • 谢谢,但它不起作用。当我尝试netstat -antu 时,没有我的端口 4201
    • npm start 是否会出现该错误?您何时会准确看到此错误?
    • 我认为这是一个后端问题,我遇到了同样的问题,当我从后端收到错误时,我意识到这个错误发生了,所以检查你的后端日志文件,我想你会找到一个错误。
    • 当我尝试登录时出现此错误。我认为问题出在我的应用程序尝试调用我的 Express Server 时。
    猜你喜欢
    • 2017-01-21
    • 2016-03-04
    • 2017-03-05
    • 2017-04-04
    • 1970-01-01
    • 2020-11-18
    • 2016-07-09
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多