Simple HTTP Server in Node.js

Here is the simple code snippy that creates a simple HTTP web server:

const http = require('http');
const fs = require('fs'); // file server setting up the default html folder
const hostname = '127.0.0.1';
const port = 3000;

fs.readFile('index.html', (err, html) =>{
   if(err) throw err;
   const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('content-type','text/heml');
      res.write(html);
      res.end;
   });
   server.listen(port, hostname, ()=>{
      console.log('Server started at port ' + port');
  });
});

That is all, and enjoy it!

If  you want to continue to develop Node web applications, install express.js is recommended, and also nodemon at the global level to keep the server run and catch the changes without stopping and restarting the server.

To use Bootstrap style sheets components, install bower from the command line and then, create a bower meta file at your web root, or wherever you want to have your CSS components pointed to:

sudo npm install -g bower

and then, create a bowerrc file, and body of the file looks like this:

{"directory": "bower_components"}

You may name any you want to replace bower_components that is supposed to be a folder name to store the bootstrap components.

Finally, you change your directory to you web root and execute the bower installation command to download bootstrap components.

bower install bootstrap

 

my signature
February 20, 2018