Breaking News

NodeJS: Develop a Static File Server

NodeJS: Develop a Static File Server

Tutorial 1: NodeJS: An Asynchronous World
Tutorial 2: Express: Middleware or more...
Tutorial 3: Understanding Routing in Express
Tutorial 4: Develop a Static File Server
Tutorial 5: Using HTTPS with Express
Tutorial 6: Develop a JSON API server
Tutorial 7: Use Templates and Views with Express
Tutorial 8: Integrating MongoDB
Tutorial 9: Testing Express Applications
Tutorial 10: Securing Express



Any web server serves a lot of static content like CSS, images, Javascript etc. files. ExpressJS provides a built-in middleware i.e. express.static to offer static content. It has a lot other features like caching mechanism, ETag support etc.

Let us try to build a static page using BootStrap framework (http://getbootstrap.com). To install BootStrap, you can use npm command i.e.

$ npm install bootstrap@3

Tutorial 4: NodeJS: Develop a Static File Server


You can check the Bootstrap structure it contains files under 'dist' folder which can be distributed.

Tutorial 4: NodeJS: Develop a Static File Server


Copy the folders of 'dist' of make soft-link of these sub-folders to your expressJS web-site folder.
Create a folder named “images”. Also, install jquery



For example, the following commands create short links of bootstrap and jquery folders inside 'public' folder.

$mkdir public
$cd ./public
$mkdir images
$ln -s ../node_modules/bootstrap/dist/css/ . 
$ln -s ../node_modules/bootstrap/dist/fonts/ . 
$ln -s ../node_modules/bootstrap/dist/js/ . 
$npm install jquery@1.12.4
$ln -s ../node_modules/jquery/dist/ .

Create a simple web page using BootStrap framework, you can check the basic templates at BootStrap Starter's Templates. Also, copy an image in 'public/image' folder.

<-- code--="" html="">
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Welcome Page</title>
    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">
  </head>

  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Simple Project</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
      <div style="margin-top: 100px;margin-bottom: 100px;margin-right: 150px;margin-left: 80px;">
        <h1>Welcome to Main Page</h1>
        <img src="./images/santa.jpg" />
        <p class="lead">ExpressJS has static middleware.<br> It serves static files (images, css, JS etc.)</p>
        <a class="btn btn-default" href="#" role="button">Login</a>
        <a class="btn btn-default" href="#" role="button">Sign</a>
      </div>

    </div><!-- /.container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
     <script src="./dist/jquery.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
  </body>
</html>


The following snippet (ex11.js) will prepare our static web server.

var express = require('express');
var app = express();


app.use(express.static(__dirname + '/public'));
app.use(function(req, res) {
 res.status(404);
 res.send("File not found!");
});
app.listen(8000, function() {
 console.log("Express server listeing on port 8000...");
});


Open browser and access http://localhost:8000/, you can see the main page is served.

Tutorial 4: NodeJS: Develop a Static File Server


No comments