JavaScript - Render HTML With Bootstrap Using Node.js

Submitted by: 
Language: 
Visitors have accessed this post 533 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Render HTML With Bootstrap Using Node.js</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <center><h2>Bootstrap has started</h2></center>
  18. <center><button type="button" class="btn btn-primary btn-lg" data-target="#form_modal" data-toggle="modal">Modal</button></center>
  19. </div>
  20. <div class="modal fade" id="form_modal" aria-hidden="true">
  21. <div class="modal-dialog">
  22. <div class="modal-content">
  23. <div class="modal-header">
  24. <h3 class="modal-title">System</h3>
  25. </div>
  26. <div class="modal-body">
  27. <center><h2>Welcome to Node.js</h2></center>
  28. </div>
  29. <div class="modal-footer">
  30. <button class="btn btn-success" data-dismiss="modal">Close</button>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <script src="js/jquery-3.2.1.min.js"></script>
  36. <script src="js/bootstrap.js"></script>
  37. </body>
  38. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.