0
votes

I'm attempting to set up my first Node.js application on Windows Server. The application runs fine with the command:

node index.js

It'll run my application on localhost:8000. However, I want it to run on a subdomain on port 80, so I tried the following command:

set PORT=80 && set HOST=api.mydomain.com && node index.js

And here is my index.js file, which handles setting the PORT and HOST in the app.listen command:

import express from 'express';
import bodyParser from 'body-parser';
const expressJwt = require('express-jwt');
const config = require('./environment.json')[process.env.NODE_ENV || 'development'];

const app = express();
const port = +process.env.PORT || 8000;
const host = process.env.HOST || 'localhost';

// Skipping app.use commands...

app.get('/', root);

app.listen(port, host, 34, err => {
  if (err) return console.error(err);
  return console.log(`Server is listening on ${port}`);
});

Unfortunately, using a subdomain errors:

events.js:187 throw er; // Unhandled 'error' event ^

Error: getaddrinfo ENOTFOUND api.mydomain.com at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) Emitted 'error' event on Server instance at: at GetAddrInfoReqWrap.doListen [as callback] (net.js:1485:12) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:17) { errno: 'ENOTFOUND', code: 'ENOTFOUND', syscall: 'getaddrinfo',
hostname: 'api.mydomain.com ' }

What is the proper way to configure the Node.js application to run under the subdomain? If I were using IIS it'd be as simple as adding additional Bindings, but now I'm in Node.js land instead so have no idea how to go about it.

2

2 Answers

0
votes

host

No for your application to run locally with this domain name you must modify the host configurations to make api.mydomaine.com correspond to the ip address 127.0.0.1 on your machine

0
votes

I ended up just running it under IIS, using a reverse proxy. Then I could use IIS to set up the subdomain binding, much simpler...

https://dev.to/petereysermans/hosting-a-node-js-application-on-windows-with-iis-as-reverse-proxy-397b