Run Node App Without Nginx
Getting Started
Node.js is one of the most popular programming languages for creating web applications. It has become so popular in part because it is easy to get started with and is highly performant. It’s also great for running applications without the need for a web server such as Nginx.
Before you can get up and running with Node.js, you need to install it on your computer. On Windows, you can use the Node.js installer available from the downloads page on their website. On MacOS and Linux, you can usually use your package manager to install Node.js. Once you have Node.js installed, you’re ready to get started.
Running Node Apps with Node’s Built-In Server
Node.js comes with a built-in web server called http-server. You can use this to run a basic web server and serve files from a folder on your computer. To start the server, you can use the http-server command.
The http-server command takes a few arguments. The first argument is the port number you want to use. The second argument is the directory you want to serve files from. The third argument is optional, but allows you to specify a different IP address for the server. By default it will listen on all available networks.
For example, if you wanted to start a server on port 8000 from the current directory, you could use the following command:
$ http-server 8001 .
You can also use the -c flag to specify a caching value for the server. This will control how long files are cached in memory before being re-downloaded from the server, which can be useful for performance.
Once you start the server, it will keep running until you shut it down. You can access the website through your browser by using the localhost IP address and the port you specified when starting the server. For example, http://localhost:8001.
Using Express to Create an API Server
Node.js comes with a framework called Express, which makes it easy to create a web server and run complex applications. Express makes it easy to create an API server, with support for routing and serving static files, as well as serving dynamic content, such as API responses.
To get started with Express, you will first need to install it. You can use the npm command to install Express:
$ npm install express
Once you have Express installed, you can use it to create an API server. To do this, you will need to create a server file, which you can name app.js. In this file, you can use the Express API structure to create endpoints and routes to serve API responses.
For example, you can use the Express Router to create an endpoint that serves a “Hello World” response. You can use the Express static middleware to serve static files, such as HTML pages, from a directory:
app.use(‘/static’, express.static(path.join(__dirname, ‘static’));
You can then use the Express listen command to start your server:
app.listen(3000, () => { console.log(‘Listening on port 3000’)});
Once you’ve saved your server file and started the server, you can access your API endpoints and the static files you’ve specified through the localhost IP address and the port you specified when starting the server.
Running Node Apps in the Background
When running a Node application, you may want to run it in the background, so that you don’t need to keep the terminal window or command prompt open. Thankfully, Node.js makes it easy to do this using the pm2 process manager.
PM2 is a process manager for Node.js applications. It allows you to start and stop processes, as well as monitor their performance. To install PM2, you can use the npm command:
$ npm install pm2 -g
Once you have PM2 installed, you can use it to start your Node application in the background. To do this, you need to first create an ecosystem file, which defines how you want your application to be run. You can then use the pm2 start command to start the application in the background:
$ pm2 start ecosystem.config.js
The application will then keep running in the background until it is stopped with the pm2 stop command. You can view the logs of your application using the pm2 log command. This can be useful when debugging because it shows you the output of the application in real time.
Using Nginx as a Reverse Proxy
Finally, you may want to use Nginx as a reverse proxy if you want to serve multiple Node applications or if you want to run a Node application in a production environment. The Nginx reverse proxy acts as a gateway for external requests to reach your Node application. This can be useful for securing your application, as well as for load balancing multiple applications.
To set up a Nginx reverse proxy, you need to create a Nginx configuration file and enable the proxy module. You will also need to enable either basic authentication or SSL encryption for a secure connection. Once that is done, you can use the proxy_pass directive to forward external requests from Nginx to your Node application.
Conclusion:
In this article, we explored how to run a Node application without using Nginx. We looked at using Node’s built-in server, Express to create an API server, running Node apps in the background with PM2, and using Nginx as a reverse proxy. We also looked at some of the advantages and disadvantages of each of these methods.
Thank You for Reading This Article
Thank you for reading this article. We hope this was helpful in understanding how to run a Node application without using Nginx. Please take some time to read other Node.js related articles here at Hackernoon. Have a great day!
FAQs
How do I install Node.js?
On Windows, you can use the Node.js installer available from the downloads page on their website. On MacOS and Linux, you can usually use your package manager to install Node.js.
What is pm2?
PM2 is a process manager for Node.js applications. It allows you to start and stop processes, as well as monitor their performance.
What is a reverse proxy?
A reverse proxy is a server that serves as a gateway for external requests to reach your Node application. This can be useful for securing your application, as well as for load balancing multiple applications.
Related Posts:
- Node Express Mongodb Nginx Digitalocean Node Express Mongodb Nginx Digitalocean Creating a Machine Through DigitalOcean Droplet Creating a DigitalOcean Droplet is the simplest and most fool-proof way of setting up a more secure and private…
- Using Helm To Install Nginx Ingress Using Helm To Install Nginx Ingress What is Helm? Helm is an open-source Kubernetes package manager. It is used to install and manage applications on Kubernetes clusters. Helm works with…
- Digitalocean Install Nginx Ubuntu 18.04 Digitalocean Install Nginx Ubuntu 18.04 Introducing DigitalOcean and Nginx DigitalOcean is a virtual cloud server provider, allowing users to spin up virtual servers located in various geographical locations throughout the…
- Install Reverse Proxy Nginx Linux Virtualbox Install Reverse Proxy Nginx Linux Virtualbox What is a Reverse Proxy? A reverse proxy is a type of server that takes requests from the Internet and forwards them to backend…
- Install Nginx Php Mysql Phpmyadmin Centos 7 Install Nginx Php Mysql Phpmyadmin Centos 7 A Brief Overview of Centos 7 Centos 7 is a Linux distribution based on the Redhat Enterprise Linux Server, and is a popular…
- Error 404 Because Server Nginx 1.14.0 Ubuntu… Error 404 Because Server Nginx 1.14.0 Ubuntu Transfer-Encoding Chunked Introduction to Error 404 Error 404 is a response code indicating that a page or file you have requested cannot be…
- Odoo Nginx Reverse Proxy Docker Odoo Nginx Reverse Proxy Docker What is Odoo? Odoo, formerly known as OpenERP and TinyERP, is an open-source Enterprise Resource Planning (ERP) software that helps businesses with their core processes…
- Apt-Get Install Nginx Php Mysql Apt-Get Install Nginx Php Mysql What is Apt-Get? Apt-Get is a powerful and innovative command line tool used by Linux-based operating systems such as Debian, Ubuntu, Linux Mint and others.…
- Remove Apache And Install Nginx Centos 7 Remove Apache And Install Nginx Centos 7 Introduction Apache is an open source HTTP server used for hosting websites and web applications on the web. Although Apache is reliable and…
- Deploy Stand Alone Vue Nginx Deploy Stand Alone Vue Nginx What is Vue.js? Vue.js is an open source JavaScript framework for building user interfaces. It was created by ex-Google employee, Evan You, and is now…
- How To Configure Php And Nginx Windows How To Configure Php And Nginx Windows Understanding Nginx and PHP Nginx (Engine X) is an open source web server. It is a powerful tool for hosting websites and web…
- How To Install Wordpress On Nginx How To Install Wordpress On Nginx Introduction If you’re looking to start your own website or blog, probably the easiest and most popular platform to choose is WordPress. WordPress is…
- Install Nginx Windows Server 2012 Install Nginx Windows Server 2012 Introduction to Nginx for Windows Server 2012 Nginx is an open source web server and a reverse proxy server that was created to address the…
- Vestacp Nginx And Apache Inactive After Migrate Ip Vestacp Nginx and Apache Inactive After Migrate IP What is Vestacp? Vesta Control Panel or VestaCP is an open-source hosting control panel. It can be freely used to manage websites,…
- Nginx Reverse Proxy Node Js < h1 > Nginx Reverse Proxy Node.js < h2 > What is Nginx Reverse Proxy? < p > A reverse proxy is a type of proxy server that takes HTTP(S)…
- Listen Default Nginx To Odoo Server Listen Default Nginx To Odoo Server What is Odoo? Odoo is an open source enterprise resource planning (ERP) software, originally released by Odoo SA in 2005, that enables businesses to…
- Start Nginx Service Centos 7 Start Nginx Service Centos 7 Before You Start: Server and Requirements If you are running a website or a web application on Centos 7, chances are you will be using…
- Php Run On Server Apache Nginx PHP Run on Server Apache Nginx What is PHP? PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source scripting language that can be used to create dynamic…
- Docker Nginx Load Config From Github Docker Nginx Load Config From Github Overview of Docker Nginx Docker Nginx is an open-source web server created by the Docker Inc. organization. This web server was designed to run…
- Reinstall Nginx Ubuntu 18.04 Reinstall Nginx Ubuntu 18.04 What is Nginx? Nginx is an open source, high-performance web server application designed to serve web traffic with lightning-fast speed and robust stability. Nginx is one…
- How To Set Rails On Nginx Ubuntu 18.04 How To Set Rails On Nginx Ubuntu 18.04 Step 1: Install Ruby Using RVM The first step for setting up Ruby on Rails on an Ubuntu 18.04 server with Nginx…
- Configure Nginx Add Module Mac Configure Nginx Add Module Mac What is Nginx? Nginx is an open source web server software that helps websites become more efficient. In particular, it helps websites by providing a…
- Centos 7 Install Nginx Php 7 Centos 7 Install Nginx Php 7 Nginx Server Overview and Prerequisites Nginx is a highly popular open source web server and reverse proxy software, known for its scalability and performance…
- How To Install Laravel On Centos 7 With Nginx How To Install Laravel On Centos 7 With Nginx What is Laravel? Laravel is an open-source framework for web development built on the model-view-controller (MVC) architectural pattern. Created in 2011…
- Nginx 1.6.2 How To Resolve Nginx 1.6.2 How To Resolve What is Nginx? Nginx (pronounced "engine x") is a web server software application. It is a highly efficient and reliable server whose primary purpose is…
- Nginx Digitalocean To Docker Node Nginx Digitalocean To Docker Node What is Nginx? Nginx is a high performance web server used to serve static content. Developed by Igor Sysoev in 2004, Nginx is now widely…
- How To Use Nginx Laravel Laragon How To Use Nginx Laravel Laragon Introduction Laragon is a powerful, lightweight, robust web server stack that is used to develop and host applications on Windows and Linux. Laragon uses…
- Centos 7 Nginx Php Worker Process And Worker Connection Centos 7 Nginx PHP Worker Process And Worker Connection What is Nginx? Nginx (pronounced "engine-x") is an open source web server software designed with high performance, stability and low memory…
- Install Nginx And Php On Ubuntu Install Nginx And Php On Ubuntu A Comprehensive Tutorial to Install Nginx And Php On Ubuntu Nginx and PHP are two of the most popular web server and scripting language…
- Node Js Nginx 502 Bad Gateway Node Js Nginx 502 Bad Gateway What is Node JS Nginx 502 Bad Gateway Error? A Node JS Nginx 502 bad gateway error is an HTTP status code that signals…