setting up node.js file server
DESCRIPTION
This document explains how to setup a node js file serverTRANSCRIPT
Setting Up A Nodejs FileServerBeing a developer we would have always come across the need for setting up an http based file server in our organization. We
would be assigned a machine and would have been asked to host a large amount of files over http. Also this machine should
keep running forever since the team knows the amount of files to be shared will keep growing over time and a large number of
users would be interested in accessing it. Okay, child’s play you say and think about setting up a Apache server for serving files.
But you being a java script developer and in love with Nodejs for quite a sometime now would want to do this same with Nodejs
right? Lets not waste time and get working. This is written for centos machine but the process would be almost the same for other
linux distributions.
Installing Nodejs
We need to run one command as root so login as root
sudo -iNow run the below command
curl -sL https://rpm.nodesource.com/setup | bash -After it finishes run the below command to exit root user
exitAs a normal user with sudo permissions install node using below command
sudo yum install -y nodejsNow run the below command to check if Nodejs is successfully installed
node -vIt would give an output something like what is given below
v0.10.38Now Node.js is successfully installed. Lets install http-server.
It will take care of starting up a server and serving files from a particular directory based on the requested file-name. We would
have had to create a nodejs application and would have had to write some code to get this functionality up and running. Http-
server is a module which will take care of all these for us. Installation is very simple. Just execute the below command
sudo npm install http-server -gNow that the http-server module is installed all that we have to do to get it running is to run the command
http-server /path/to/directoryIn the above command replace the /path/to/directory to the path in which you have your files. In my case i had used
http-server /home/vivbabuNow from a browser try to access any files. I had a file name abc.txt in the /home/vivbabu directory. If someone wants to access
that file they can use
http://192.168.1.6:8080/abc.txt
VIVEK BABUSHREYAS, HOUSE NO 6, FLAT NO 2, BBMP NO 217, 4TH CROSS VIGNAN NAGAR
BANGALORE - 560075.EMAIL: [email protected]. PHONE: +91 81297 40812
Now we have the web server up and running. The only problem here is that if we have logged in via putty or any other session if
that session is ended the web server also is stopped. What if we want to keep this web server running forever and keep serving
files forever. The answer is forever. There is a nodejs forever service which helps us to do the same.
First we have to install forever
sudo npm install foreverOnce installation is done we can start http-server forever using the below command
forever start /usr/lib/node_modules/http-server/bin/http-server /home/vivbabuThe http-server would start forever with an output as given below
warn: --minUptime not set. Defaulting to: 1000mswarn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000msinfo: Forever processing file: /usr/lib/node_modules/http-server/bin/http-serverWe can see the list of tasks running forever using this command
forever listIn our case we would get an output as given below
info: Forever processes runningdata: uid command script forever pid id logfile uptime data: [0] MFVc /usr/bin/node /usr/lib/node_modules/http-server/bin/http-server /home/vivbabu 20712 20714 /home/vivbabu/.forever/MFVc.log 0:0:6:13.36 We can see the http-server logs using the below command
tail -f /home/vivbabu/.forever/MFVc.logTry downloading more and more files we can see the logs for those events.
Now we have all the requirements that we started with. An http-server serving files for ever. If you want to stop the http-server find
the forever pid for that process and stop it using the forever stop command. In our case do the below step
VIVEK BABUSHREYAS, HOUSE NO 6, FLAT NO 2, BBMP NO 217, 4TH CROSS VIGNAN NAGAR
BANGALORE - 560075.EMAIL: [email protected]. PHONE: +91 81297 40812