简体   繁体   English

当我通过浏览器打开html页面时,为什么倒计时应用程序可以正常运行,但是当我使用http服务器时却不能正常运行?

[英]Why does my countdown app work fine when I open the html page through the browser, but not when I use an http server?

The app is fully functioning when I open the html file in my browser, but when I try and open it on localhost:3000 it only loads the html and doesn't pick up the css or javascript. 当我在浏览器中打开html文件时,该应用程序可以正常运行,但是当我尝试在localhost:3000上打开该文件时,该应用程序只会加载html,而不会提取CSS或javascript。

HTML: HTML:

<html>
  <head>
<link href="./main.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="del-countdown">
     <h1>THE SINGULARITY IS NEAR</h1>
     <div id="clock"></div>
      <div id="units">
       <span>Years</span>
       <span>Days</span>
       <span>Hours</span>
       <span>Minutes</span>
       <span>Seconds</span>
      </div>
    </div>
  <script src="../index.js"></script>
 </body>
</html>

CSS: CSS:

body{
  background: #282e3a;
  font-family: tahoma;
}
h1{
  color: #fff;
  text-align: center;
  font-size: 74px;
  letter-spacing: 10px;
  margin-bottom: 5px;
}
h3{
  color: #fff;
  text-align: center;
  font-size: 36px;
  letter-spacing: 5px;
  margin-top: 5px;
}
#del-countdown{
  width: 850px;
  margin: 15% auto;
}
#clock span{
  float: left;
  text-align: center;
  font-size: 84px;
  margin: 0 2%;
  color: #fff;
  padding: 20px;
  width: 16%;
  border-radius: 20px;
  box-sizing: border-box;
}
#clock span:nth-child(1){
  background: #696868;
}
#clock span:nth-child(2){
  background: #7D7C7C;
}
#clock span:nth-child(3){
  background: #9E9E9E;
}
#clock span:nth-child(4){
  background: #C4C4C4;
}
#clock span:nth-child(5){
  background: #D9D7D7;
}
#clock:after{
  content: "";
  display: block;
  clear: both;
}
#units span{
  float: left;
  width: 20%;
  text-align: center;
  margin-top: 30px;
  color: #ddd;
  text-transform: uppercase;
  font-size: 13px;
  letter-spacing: 2px;
  text-shadow: 1px 1px 1px rgba(10,10,10, 0.7);
}

span.turn{
  animation: turn 0.7s ease forwards;
}

@keyframes turn{
  0%{transform: rotateX(0deg)}
  100%{transform: rotateX(360deg)}
}

Index.js: Index.js:

function updateTimer(deadline){
  var time = deadline - new Date();
  return {
    'years': Math.floor( time/(1000*60*60*24*365) ),
    'days': Math.floor( time/(1000*60*60*24) % 365 ),
    'hours': Math.floor( (time/(1000*60*60)) % 24 ),
    'minutes': Math.floor( (time/1000/60) % 60 ),
    'seconds': Math.floor( (time/1000) % 60 ),
    'total' : time
  };
}


function animateClock(span){
  span.className = "turn";
  setTimeout(function(){
    span.className = "";
  },700);
}

function startTimer(id, deadline){
  var timerInterval = setInterval(function(){
    var clock = document.getElementById(id);
    var timer = updateTimer(deadline);

    clock.innerHTML = '<span>' + timer.years + '</span>'
                    + '<span>' + timer.days + '</span>'
                    + '<span>' + timer.hours + '</span>'
                    + '<span>' + timer.minutes + '</span>'
                    + '<span>' + timer.seconds + '</span>';

    //animations
    var spans = clock.getElementsByTagName("span");
    animateClock(spans[4]);
    if(timer.seconds == 59) animateClock(spans[3]);
    if(timer.minutes == 59 && timer.seconds == 59) animateClock(spans[2]);
    if(timer.hours == 23 && timer.minutes == 59 && timer.seconds == 59) animateClock(spans[1]);
    if(timer.days == 364 && timer.hours == 23 && timer.minutes == 59 && timer.seconds == 59) animateClock(spans[0]);

    //check for end of timer
    if(timer.total < 1){
      clearInterval(timerInterval);
      clock.innerHTML = '<span>0</span><span>0</span><span>0</span><span>0</span><span>0</span>';
    }


  }, 1000);
}


window.onload = function(){
  var deadline = new Date("January 01, 2045 00:00:01");
  startTimer("clock", deadline);
};

Server.js Server.js

var http = require('http')
var fs = require('fs')
var path = require('path')

//404 response
function send404Response(res) {
  res.writeHead(404, {"Content-Type": "text/plain"})
  res.write("Error 404: Page not found!")
  res.end()
}

//Handle user request
function onRequest(req, res) {
  if(req.method == 'GET' && req.url == '/') {
    res.writeHead(200, {"Content-Type": "text/html"})
    fs.createReadStream("./public/index.html").pipe(res)
  }
  else{
    send404Response(res)
  }
}

http.createServer(onRequest).listen(3000)
console.log("Server is now running...");

put the js and css file in a folder named pubilc. 将js和css文件放在名为pubilc的文件夹中。 And call app.use('/public',express.static(__dirname + '/public')); 并调用app.use('/public',express.static(__dirname + '/public')); Now add css and js src="/public/js" 现在添加css和js src="/public/js"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我通过Node.js中的“ http”服务器提供服务时,html页面中的脚本不起作用 - Script in my html page not working when i serve it through the 'http' server in Node.js 通过节点服务器提供HTML时,为什么页面Javascript不起作用? - Why is my page Javascript not working when I serve my HTML through a Node server? 为什么我的代码只能在html中工作,而在将其放入Javascript页面时却不能工作? - Why does my code only works in html but does not work when I put it into a Javascript page? 为什么当我降级node_modules时我的React应用程序可以正常工作,但是更新时却失败了? - Why does my React app works fine when i downgrade node_modules but fails when update it? 为什么我使用 Datatables 的 Rails 应用程序在我使用后退按钮时无法正确绘制表格,但在我刷新页面时却如此? - Why does my rails app that uses Datatables not draw tables correctly when I use the back button, but does when I refresh the page? 添加路由后,为什么我的AngularJS应用无法正常工作? - Why does my AngularJS app not work when I add routing? 使用锚点时,为什么按键处理程序不起作用 - Why does my keypress handler not work when I use anchors 为什么打开浏览器时jQuery无法正常工作/无法显示? - Why is my jQuery not working/showing when I open my browser? 当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有 - Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+” 为什么我在 Postman 中的 POST 请求工作正常,但不在客户端(浏览器)上? - Why does my POST request in Postman work fine but when not on the client side (browser)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM