title: Deploying an Angular Build on Nginx for Windows
body: <p>I'm trying to deploy my Angular Project's build (which also has a REST API in it) on nginx, but the localhost refuses to connect when I load the Dist inside the html folder.</p>
  
  <p>A localhost run using npm run build does listen to it, but I cannon seem to crack the code on how to deploy that exact output onto the Nginx webserver.</p>
  
  <p>(Inside my Angular Project)
  server.js</p>
  
  <pre><code>// Get dependencies
  const express = require('express');
  const path = require('path');
  const http = require('http');
  const bodyParser = require('body-parser');
  
  // Get our API routes
  const api = require('./server/routes/api');
  
  const app = express();
  
  // Parsers for POST data
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  
  // Point static path to dist
  app.use(express.static(path.join(__dirname, 'dist')));
  
  // Set our api routes
  app.use('/api', api);
  
  // Catch all other routes and return the index file
  app.get('*', (req, res) =&gt; {
    res.sendFile(path.join(__dirname, 'dist/ProjectName/index.html'));
  });
  
  /**
   * Get port from environment and store in Express.
   */
  const port = process.env.PORT || '3000';
  app.set('port', port);
  
  /**
   * Create HTTP server.
   */
  const server = http.createServer(app);
  
  /**
   * Listen on provided port, on all network interfaces.
   */
  server.listen(port, () =&gt; console.log(`API running on localhost:${port}`));
  </code></pre>
  
  <p>server/routes/api (for testing)</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  
  // declare axios for making http requests
  const axios = require('axios');
  const API = 'https://jsonplaceholder.typicode.com';
  
  /* GET api listing. */
  router.get('/', (req, res) =&gt; {
    res.send('api works');
  });
  
  // Get all posts
  router.get('/posts', (req, res) =&gt; {
    // Get posts from the mock api
    // This should ideally be replaced with a service that connects to MongoDB
    axios.get(`${API}/posts`)
      .then(posts =&gt; {
        res.status(200).json(posts.data);
      })
      .catch(error =&gt; {
        res.status(500).send(error)
      });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>package.json script</p>
  
  <pre><code>"scripts": {
      "ng": "ng",
      "start": "ng serve --proxy-config proxyConfig.json",
      "build": "ng build &amp;&amp; node server.js",
      "test": "ng test",
      "lint": "ng lint",
      "e2e": "ng e2e"
    }
  </code></pre>
  
  <p>proxyConfig.json</p>
  
  <pre><code>{
      "/api": {
        "target": "http://localhost:3000",
        "secure": false,
        "changeOrigin": true
      }
    }
  </code></pre>
  
  <p>nginx.conf</p>
  
  <pre><code>server {
          listen       8080;
          server_name  localhost;
  
          #charset koi8-r;
  
          #access_log  logs/host.access.log  main;
  
          location / {
              root   html/dist/ProjectName;
              index  index.html index.htm;
          }
      location /api {
                   proxy_pass   http://localhost:3000;
  </code></pre>