Run Node App Without Nginx


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.