How to serve static files with node.js

In front end development to serve static files such as images, CSS files, and JavaScript files you require the web servers. and you need a server like apache or nginx servers to server files.Installing http server and configuration is much hactic for a frontend developers.With the help of node.js we can easily create a http web server.if you are using node.js environment then there is quick configuration package for HTTP server without writing a single line of code.

Node.js


Assuming you have Node.js installed in your machine.If node.js not installed please read this article Getting started with node.js. You need to install the module called http-server using the following command.


npm install -g http-server

That’s it. Now you can serve any static files and assets using this module. Now you switch to any folder and run the server using the following command and your folder will act as server and it will serve static files such as images, CSS files, and JavaScript files.


~/Desktop mkdir assets && cd $_
~/Desktop/assets http-server

Now assets folder will act like root of the Http server.Please feel free to comment below for suggestions and problems.



No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular