JavaScript - Render HTML With Bootstrap Using Node.js

Language

In this tutorial we will create a Render HTML With Bootstrap Using Node.js. The Node.js is a run-time environment includes everything you need to execute a program written in JavaScript. It is an open source, cross-platform runtime environment for developing server-side and networking applications. It is widely use by developers because it provide multiple libraries that simplifies the web development.

Getting started

First you will need to download & install the Node.js, here's the link https://nodejs.org/en/. After Node.js is installed, open the command prompt then type "npm install -g nodemon", and hit enter. tut1 This will install nodemon, a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

The Main Interface

This code contains the interface of the application. This code will render application and display the form. First cd to your working directory and write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Sourcecodester</title>
  5. <link type='text/css' href='css/bootstrap.css' rel='stylesheet' />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Render HTML With Bootstrap Using Node.js</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <center><h2>Bootstrap has started</h2></center>
  17. <center><button type="button" class="btn btn-primary btn-lg" data-target="#form_modal" data-toggle="modal">Modal</button></center>
  18. </div>
  19. <div class="modal fade" id="form_modal" aria-hidden="true">
  20. <div class="modal-dialog">
  21. <div class="modal-content">
  22. <div class="modal-header">
  23. <h3 class="modal-title">System</h3>
  24. </div>
  25. <div class="modal-body">
  26. <center><h2>Welcome to Node.js</h2></center>
  27. </div>
  28. <div class="modal-footer">
  29. <button class="btn btn-success" data-dismiss="modal">Close</button>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <script src="js/jquery-3.2.1.min.js"></script>
  35. <script src="js/bootstrap.js"></script>
  36. </body>
  37. </html>

Creating the Script

This code contains the main function of the application. This code will create a server side script that let you access your website locally. To that just kindly copy and write these block of codes inside the text editor, then save it to your working directory as server.js.
  1. var http = require('http');
  2. var fs = require('fs');
  3. var url = require('url');
  4. var path = require('path');
  5. var server = http.createServer(function(request, response) {
  6. var pathname = url.parse(request.url).pathname;
  7. var ext = path.extname(pathname);
  8. if(ext){
  9. if(ext != '.ico'){
  10. if(ext === '.css'){
  11. response.writeHead(200, {"Content-Type": "text/css"});
  12. }else if(ext === '.js'){
  13. response.writeHead(200, {"Content-Type": "text/js"});
  14. }
  15. response.write(fs.readFileSync(__dirname + pathname, 'utf8'));
  16. }
  17.  
  18. }else{
  19. response.writeHead(200, {"Content-Type": "text/html"});
  20.  
  21. response.write(fs.readFileSync("index.html", "utf8"));
  22. }
  23.  
  24. response.end();
  25.  
  26. }).listen(8080)
There you have it we successfully created a Render HTML With Bootstrap Using Node.js. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment