Monday 8 May 2017

Deploy to production MEAN app (Angular2)

I am trying deploy my first angular 2 app with mongodb/express on my own server (debian with nginx )

I have installed everything like nginx, mongo, node, build-essentials, pm2 etc

I am starting my server.js by pm2 start server.js -x -- --prod

my server.js file

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

This is my config.json

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

My front-end is in angular2/4 I also used ng build --prod for create my dist directory. My path to dist on server is /srv/mydomain/dist

And my server part is inside /srv/mydomain/server

Whem I am open my app I see my front end with login

After typing login and password console shows errors

OPTIONS http://localhost:4000/users/authenticate net::ERR_CONNECTION_REFUSED XHR failed loading: POST "http://localhost:4000/users/authenticate".

I don't know why is 4000 instead of 3008?

There is my 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;
}

}

What I am doing wrong?



via Defus

No comments:

Post a Comment