02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Using http-server as a Quick Server Solution

Using http-server as a Quick Server Solution

In this tutorial, we will explore how to use http-server as a quick solution for serving files locally with minimal setup. http-server is a simple, zero-configuration command-line tool that you can use to quickly serve static files. This is particularly useful for testing or prototyping projects without the need for a complex server setup.

Step 1: Installing http-server

To use http-server, you need to install it globally on your system. You can do this using NPM (Node Package Manager). Run the following command in your terminal:

npm install -g http-server

The -g flag installs http-server globally, which means you can use it from any directory on your system.

Critical Update (2024): In newer versions of Node.js, it's recommended to use the latest npm version to manage global packages securely. Consider updating npm before installing http-server to ensure a smooth installation.

Step 2: Running http-server

Navigate to the directory containing the files you want to serve. For example, if you have a folder called my-app with some HTML, CSS, and JavaScript files, use the cd command to go to that directory:

cd my-app

Once you are in the correct directory, run http-server by typing:

http-server

By default, http-server will start a server on port 8080 and make all the files in the current directory accessible through a web browser.

Example Output

After running http-server, you will see output similar to this:

Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://192.168.1.10:8080
Hit CTRL-C to stop the server

Step 3: Accessing the Server

To view your files, open a web browser and go to http://127.0.0.1:8080/. You should see your project's files being served locally. This is a quick and effective way to test your static HTML, CSS, and JavaScript files during development.

Step 4: Specifying a Custom Port

By default, http-server serves files on port 8080. However, you can specify a custom port using the -p flag:

http-server -p 3000

This command will start the server on port 3000 instead of the default 8080.

Step 5: Stopping the Server

To stop the server, simply press CTRL + C in the terminal where the server is running. This will terminate the http-server process.

Benefits of Using http-server

  • Quick Setup: http-server requires minimal configuration, making it perfect for quickly testing static sites.
  • Lightweight: Unlike full-fledged web servers, http-server is lightweight and easy to use.
  • Cross-Platform: Since http-server is a Node.js package, it works across all major platforms (Windows, macOS, and Linux).

Tip: While http-server is great for local development and quick prototyping, it is not intended for use in production environments. For production, you should use more robust server solutions like Express, NGINX, or Apache.

Conclusion

In this tutorial, we learned how to use http-server as a quick solution for serving static files locally. This lightweight tool is an ideal choice for quickly testing your projects and ensuring everything works before deploying. With just a few commands, you can have a local server running, allowing you to see your project in action.

Next Steps: After using http-server for testing, consider transitioning to a more complex server setup, such as Express, for larger projects that require more functionality, such as handling dynamic routes or connecting to a database.

Video Transcript

00:00In the last lecture, we saw how
00:02to use the native capabilities available in node js
00:05to create a server.
00:07In this lecture, we're gonna depend on an external third
00:10party module, which is called the HTTP server.
00:14With it, we're gonna go ahead
00:16and use that module to create a server,
00:18but it's gonna be a static site.
00:20So it's gonna be very easy for us if we have static sites
00:23to set up and run a server very, very quickly.
00:25So let's jump right into it.
00:28At this stage, we have a server, but we have a problem
00:31because our server is really not doing anything really
00:34useful and we really want to be able to hit our server
00:37and fetch static files.
00:39And that's really the point of the last
00:41lecture in this chapter.
00:42Whereas in the next chapter, we're moving into dynamic data
00:46with the help of express js.
00:49Now, before we get to that stage, let's go ahead
00:52and, uh, this server shouldn't be working
00:53'cause it's been a really long time since I started it.
00:56Let's go ahead and what I've done,
00:57I went into our source files, into our JavaScript
01:00and just commented all out
01:01because we're gonna create a static server using
01:05another module, well, actually another package
01:08as we talked about the difference
01:09between packages and modules.
01:11Um, I have also created an HTML document
01:15that just has some static copy inside of it.
01:17So literally, if we just look at here, um,
01:20all we really have here is a package.
01:23We have the JavaScript, which is currently really empty,
01:25and we have this new HTML document
01:27that just has a little bit of HTML
01:30and we want it to actually run on our server.
01:33Alright, so let's get ahead, let's make it happen.
01:36And to do that, we're gonna use H TT P server.
01:39But what I wanna do is I want to install it globally
01:41because it's gonna be our server.
01:43This is not something that we need specifically,
01:45it's not a package or a dependency of our application,
01:49but on the other hand, it is actually a global means of way
01:53that we're gonna be installing
01:55and basically setting up servers.
01:57So I'm gonna go ahead and install
01:59that into the global scope.
02:01So I'm gonna ask from NNPM to install H TT P server
02:05into the global scope.
02:07Once I do that, it's gonna go through
02:09and I, I probably had a typo.
02:11So let's go ahead and do this again.
02:13NPM install HTT P server
02:18into the global scope.
02:20Let's give it one more shot and here we go.
02:22It's now installing HTT P server.
02:24And notice all the dependencies that are added in.
02:27And remember, we've talked a lot about dependencies,
02:30different levels, so make sure if it's a,
02:32if it looks unfamiliar, go back to our NPM section,
02:35our chapter about NPM.
02:36Alright, so now that we have it set up
02:38and we've installed HTP C-H-T-T-P server, all
02:42that's really left for us to do is to start it up.
02:44And to start it up, I'm just gonna call HTT P Server.
02:48I'm gonna say, where is our server?
02:50In this case, I want to set it to be exactly where we are
02:53inside of our current folder.
02:55And what else do I wanna do? I wanna set our alias to
02:58Be a local host, so we could approach it as local host
03:02and I need its oso set and a IP address
03:06or a port more accurately, sorry.
03:08And for our port, I'm gonna go ahead and do 3004
03:12because we've been playing around already
03:13with the three thousands, just the number
03:15that our system is not using.
03:17So I'm gonna go ahead and click here on enter,
03:20and once I click on enter, it's gonna start up my server.
03:23At any point, I could stop the server by clicking on Ctrl C,
03:26but now that the server is running, I could literally go
03:29to my browser and,
03:30and go to the path that's set inside of our HTTP, inside
03:34of our, uh, our host that we've created.
03:37And once I've do, I do that I should gain direct access to
03:41that new HTML file that I've have there positioned.
03:43And if I try to go to something that doesn't exist,
03:46it's gonna not show us anything, which is beautiful.
03:49So we've already, we basically set up a server
03:52and we're getting our content
03:53and a basic simple server we could have running within
03:56seconds, literally.
03:57Now this is not really the power of no js,
04:01but it's really a great way
04:03for you if you know you're creating a static HTML, uh,
04:06client side server,
04:07then you could very quickly create a server this way,
04:09even in a production environment.
04:11To learn more about HTT HTTP server and other configurations
04:14and other things you can do with it,
04:17go into the documentation of this chapter.
04:19I've added Otto a reference to HTT P server,
04:22so you could look into its documentation,
04:25but Otto in general, you could always go into the path from
04:28NPM where it's installed.
04:30And in most packages you'll have a little bit
04:32of documentation in there as well
04:34to give you more information about those packages.
04:37Now that we know how to set up a server,
04:39a very basic server, but with that,
04:41we have a local host running.
04:43In the next lecture, we're gonna go back into React
04:46and see how to connect, react where we don't need
04:48that special configuration security hack
04:51that we created on our local browser
04:53because we didn't have a server running.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!