部署到生产MEAN应用程序(Angular2)

时间:2017-05-08 18:28:27

标签: node.js angular express nginx mean-stack

我正在尝试使用mongodb / express在我自己的服务器上部署我的第一个角度2应用程序(debian with nginx)

我安装了nginx,mongo,node,build-essentials,pm2等所有内容

我正在通过pm2 start server.js -x - --prod

启动我的server.js.

我的server.js文件

require('rootpath')();
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var config = require('config.json');

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// use JWT auth to secure the api
app.use(expressJwt({ secret: config.secret }).unless({ path: ['/users/authenticate', '/users/register'] }));

// routes
app.use('/users', require('./controllers/users.controller'));

// start server
app.listen(3008, function() {
    console.log('server na porcie 3008!')
});

这是我的config.json

{
    "connectionString": "mongodb://localhost:27017/mydb",
    "apiUrl": "http://127.0.0.1:3008",
    "secret": "testing testing"
}

我的前端是angular2 / 4我还使用了ng build --prod来创建我的dist目录。我在服务器上的dist路径是/ srv / mydomain / dist

我的服务器部分在/ srv / mydomain / server

我打开我的应用程序我看到我的前端登录

输入登录名和密码后,控制台显示错误

  

选项http://localhost:4000/users/authenticate net :: ERR_CONNECTION_REFUSED   XHR加载失败:POST“http://localhost:4000/users/authenticate”。

我不知道为什么4000而不是3008?

有我的nginx conf

server {
    listen 80;

    # Web
    root /srv/myapp/dist;
    location / {
        try_files $uri /index.html;
    }

    # Api
    location /srv/myapp/server {
        proxy_pass http://127.0.0.1:3008;
    }
}

我做错了什么?

修改

这是我的authentication.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { AppConfig } from '../app.config';

@Injectable()
export class AuthenticationService {
    constructor(private http: Http, private config: AppConfig) { }

    login(username: string, password: string) {
        //console.log(this.config.apiUrl);
        return this.http.post(this.config.apiUrl + '/users/authenticate', { username: username, password: password })
            .map((response: Response) => {
                // login successful if there's a jwt token in the response
                let user = response.json();
                if (user && user.token) {
                    // store user details and jwt token in local storage to keep user logged in between page refreshes
                    localStorage.setItem('currentUser', JSON.stringify(user));
                }
            });
    }

    logout() {
        // remove user from local storage to log user out
        localStorage.removeItem('currentUser');
    }
}

0 个答案:

没有答案