
It is a bit more powerful, but also complicated, so it may not be suited for beginners. It also has a built-in load balancer for improved uptime. PM2 is an alternative to forever that keeps apps running between server restarts. It also restarts your app after a crash, making it useful for monitoring and restarting Node servers. forever will help you keep Node servers running even after the operating system is rebooted. Keeping Servers Running with foreverĪnother scenario arises when you have a running Node.js server that you want to keep running automatically. This Node.js serving option is useful for serving a simple app that does mainly front-end work. Visit to verify that the server is running and serves our file with the "Hello World" message. Then you can run the http-server using the command: $ http-server. Create a static HTML file inside this public directory named index.html with the following contents: Hello from http-server Hello, World! In a fresh directory, create a child directory named public in which we will place static files for http-server to serve. To use http-server, install it with the command npm install http-server -g. In this case you need to install the http-server zero-configuration, command-line server to serve your files. If all you want is a Node.js server to serve HTML and serve static files without you coding anything for the server, Node has a solution for that as well. In your browser, you should see the "Hello world!" text. Then create a file app.js with the following contents: // app.js const server = require( 'server') In a fresh project, install the module with the command npm install server -save.

Note that this module requires Node version 7.6.0 or later. We can also start a Node server using the server npm module. Other Options for Starting a Server with Node.js Using Other Server Modules Now, when you restart the server and visit the app in the browser, you will see that our server is now streaming a video file.

Read the video into a stream let vidstream = fs.createReadStream( 'assets/Yngwie_Malmsteen_interview.mp4') Ĭonsole.log( 'Node server running on port 3000') Create an instance of the http server to handle HTTP requests let app = http.createServer( ( req, res) => )

In a file app.js, create and save the following server-creation code: // app.js const http = require( 'http') It provides the interaction between users and your application.Ĭreating and starting a server is easy with Node.js's built-in http module. Did you know that there are multiple ways to start a Node.js server and keep it running? In this post, we will explore various ways to start an HTTP Node server.Ī Node.js server makes your app available to serve HTTP requests.
