User:Tsu2/NodeJs http servers
This page documents how to instantiate web servers using NodeJS. Most scripting languages support invoking a small webserver for temporary tasks like Developing code, testing and staging websites and supporting Static Sites in general.
Of course, everything on this page assumes you have installed NodeJS already
Before Anything Else
If you haven't already done so, update your node package management "npm." The version installed from anywhere other than the official NodeJS repos is likely old
npm i npm
The HTTP Servers
1. http-server
Install
npm http-server
Navigate to your website root and run
cd website_root
http-server
By default, your website should be available at http://localhost:4000
2. connect and serve-static
Install
npm install connect serve-static
Create your server.js configuration file in an easily accessible location. Running the following in a console or script should create your file in your home directory root
cat > ~/server.js << EOL
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8080, function(){
console.log('Server running on 8080...');
});
EOL
Now you can reference server.js from the root of any website code, for example
cd website_root
node ~/server.js
By default, your website should be available at http://localhost:8080
Additional
The above and similar node web servers are generally unusable "as-is" in a Production setting, their defaults are generally minimal and lightweight, suitable for on-the-fly invoking for a single or few clients, and in a protected network. If you want to deploy for Production use, NGINX is highly advisable but you can also tweak the above node servers for better performance and security. One article that describes some of these tweaks is at the following
http://stackabuse.com/node-http-servers-for-static-file-serving/