1

I'm new to the whole Node JS community and I'm trying to build a test app using MERN (Mongo DB, Express, React, Node JS). I'm coming from the LAMP stack (Linux, Apache, MySQL, and PHP).

  1. I've set up my node.js server on Ubuntu 18 (AWS EC2) and I've installed Node JS, Express, and Mongo DB. From my understanding Express is a web server kind of like Apache or Nginx? So can't it replace Nginx and Apache? I see some tutorials that use Nginx with Express, I'm not sure why.

  2. Also, how do I connect my domain (example.com) to my Node JS server? All the examples I see are with localhost on port 3000 or 3001. When I worked with Apache, all I had to do was point the domain to the IP address, then go into the 000-default.conf file (apache config file) and add the domain and directory for the domain to go to. But now when I point the domain to my Node server, it just says "refused to connect". Am I doing this correctly? I have my website application files in the folder "/var/app".

Example of the config from Apache:

<VirtualHost *:80>
    DocumentRoot /var/www/html

    # ...
</VirtualHost>

Thanks for the help all!

pixie123
  • 929
  • 3
  • 14
  • 27

2 Answers2

1

I will try to help you for your second question. If you want to use an http server with nodejs, you have 2 possibilities:

  • You will change the port in express listen() method call by using port 80 like that :

    app.listen(80, () => console.log('Server running on port 80!'))
    

With this way, you cannot run nginx or apache on port 80 simultaneously.

  • You will use nginx or apache as a reverse proxy which forward http request from your domain on port 80 to the port 3000 if your app is built with the port 3000. For instance with a nginx config :

    server {
        listen 80;
        server_name myapp.domain.com;
    
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    

Hope this will help you.

chaillouvincent
  • 197
  • 1
  • 3
  • 17
  • Thank you for this! But is Nginx or Apache needed? I thought Express was able to handle this? – pixie123 Nov 01 '19 at 16:28
  • Yes, you can use node with express only. For instance, if you don't need to use apache or nginx to manage another websites or apps on your server (dedicated, vps or other), you can use only node with express or another framework. Please, keep in mind that you can use only one http server on port 80. You can check some answers in this post. https://stackoverflow.com/questions/10216003/how-to-make-node-js-multi-tenant-for-websites-on-port-80 You will learn that a reverse proxy can be interesting for performance and managment features. – chaillouvincent Nov 01 '19 at 16:38
0

Express js is just a framework on node js. Node js comes with its own web server and provides 'http' library (The one that Express just wraps).

Assuming you already run npm init and npm install --save express. All what you need to run your server is the following:

server.js

const express = require('express')

// Create Express app
const app = express()

// A sample route
app.get('/', (req, res) => res.send('Hello World!'))

// Start the Express server
app.listen(3000, () => console.log('Server running on port 3000!'))

then run:

node app.js

Your terminal should show output: 'Server running on port 3000!' and when you open your browser on http://localhost:3000 it will shows 'Hello World!'

MEDZ
  • 2,227
  • 2
  • 14
  • 18
  • Thank you but how can I connect my domain (example.com) instead of of localhost? – pixie123 Nov 01 '19 at 16:04
  • Do you point the domain name to your EC2 instance? If you didn't do that, then follow the steps in this guild: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-ec2-instance.html – MEDZ Nov 01 '19 at 16:10
  • Yes I did, all I'm getting is a "refused connection" in my browser. – pixie123 Nov 01 '19 at 16:27