Vibe.d tutorial
  • Build web apps with Vibe.d
  • Setting up
  • The default hello world app
  • Use your own HTML page
  • Serving other static files
  • Diet templates
  • More template pages
  • Use your own functions
  • Templates for ease of maintenance
  • Using include in templates
  • Responsive layout using CSS Grid
  • A fixed navbar and sticky footer
  • CSS modal dialogues
  • The web interface
  • Setting up MySQL server and tools
  • The schema
  • Setting up MySQL for Vibe.d
  • The EmployeeController class
  • The EmployeeModel class
  • A form for adding a new employee
  • Saving form data into the database
  • Testing the whole thing
  • Listing all the employees
  • Retrieving a record for editing
  • Saving form changes to the database
  • Deleting a record from the database
  • Finding an employee record by name
  • Displaying error messages with _error
  • Authentication and authorization
  • Logging in and authentication
  • Saving the login state to the session
  • Enforcing authorization through the session
  • Logging out
  • All the sources so far
  • A new project
    • The timekeeping system
Powered by GitBook
On this page

Serving other static files

What if an HTML page requires CSS and JavaScript files? Where do we place them?

Create views\helloagain.html with the following code:

<!DOCTYPE html>
<html>
  <head>
    <title>My webpage!</title>
    <link rel="stylesheet" href="css/helloagain.css" />
    <script async src="js/helloagain.js"></script>
  </head>
  <body>
    <h1>Hello, World!</h1>
    <h4 id='date'></h4>
    <div class="image-section">
      <div class="section-style">
        <img src="https://source.unsplash.com/random/400x200" alt="" />
        <p>A  random image courtesy of unsplash.com.</p>
      </div>
      <div class="section-style">
        <img src="https://source.unsplash.com/random/400x200"alt=""/>
        <p>A random image courtesy of unsplash.com.</p>
      </div>
    </div>
    <divclass="image-section">
      <divclass="section-style">
        <img src="https://source.unsplash.com/random/400x200" alt="" />
        <p>A random image courtesy of unsplash.com.</p>
      </div>
      <div class="section-style">
        <img src="https://source.unsplash.com/random/400x200" alt="" />
        <p>A random image courtesy of unsplash.com.</p>
      </div>
    </div>
  </body>
</html>

Actually, I got this from the wild wild web courtesy of Programming Liftoff

Create a css\ folder inside the public\ folder (you can right-click on it)

Then create the public\css\helloagain.css stylesheet file (also from Programming Liftoff) inside that new folder.

body 
{
  text-align: center;
  background-color: #f0e8c5;
}
div 
{
  margin-top: 15px;
}
.image-section 
{
  display: flex;
  justify-content: center;
}
.section-style 
{
  margin-right: 25px;
  margin-left: 25px;
  background-color: white;
}

Next, create the public\js folder.

Then create the public\js\helloagain.js with the following contents (also from Programming Liftoff)

document.getElementById('date').innerHTML = new Date().toDateString();

Then edit source\app.d to use a router

import vibe.vibe;
void main()
{
  auto settings = new HTTPServerSettings;
  settings.port = 8080;
  settings.bindAddresses = ["::1", "127.0.0.1"];
  auto router = new URLRouter;
  router.get("/", staticTemplate!"helloagain.html");
  router.get("*", serveStaticFiles("public/"));
  auto listener = listenHTTP(settings, router);
  scope (exit) listener.stopListening();
  runApplication();
}

The line

  router.get("/", staticTemplate!"helloagain.html");

means display helloagain.html when the URL simply shows “/”. This means the helloagain.html is the index page or home page. The “/” URL expands to

http://localhost:8080/

which is the root of the application.

The line

  router.get("*", serveStaticFiles("public/"));

means for all other files not indicated by any route, treat them as static files and look for them inside the public/ folder.

We indicated in the helloagain.html page that the stylesheet and the javascript files are in /public/css/ and /public/js/ but without mentioning the public/ folder:

    <link rel="stylesheet" href="css/helloagain.css" />
    <script async src="js/helloagain.js"></script>

We do not mention the public/ folder in the path.

After refreshing the browser, something similar is what you will see:

The images you see will most likely be different as they are randomly selected every time you refresh the page, so try to refresh the page a few times to see a different set of pictures.

PreviousUse your own HTML pageNextDiet templates

Last updated 2 years ago

()

https://dev.to/programliftoff/create-a-basic-webpage-with-css-and-javascript--104i