Express js不提供静态文件

时间:2016-06-05 07:00:33

标签: javascript node.js express

我是新来表达js.I认为这是一个新手问题.express js不提供静态文件

var express = require('express');
express()
    .set('view engine','ejs')
    .use(express.static('public'))
    .get('/',function(req,res){
      res.render('index');
    })
    .listen(3000);

Error Project Structure

1 个答案:

答案 0 :(得分:2)

我认为您需要使用:

 var data,options,chart;
     var fdata;
     var mons=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"];
    function loadChart(){
    timezoneJS.timezone.zoneFileBasePath = "tz";
        timezoneJS.timezone.defaultZoneFile = [];
        timezoneJS.timezone.init({async: false});
  var da = readTextFileNow();
  console.log(da.length);
  var t = "27-May-2016:19:00:00";
  var etot=[];var e200=[];var e400=[];var e415=[];var e500=[];var e502=[];var e503=[];var etotCount=0;var e200Count=0;var e400Count=0;var e415Count=0;var e500Count=0;var e502Count=0;var e503Count=0;
  for(var i=0;i<da.length;i++){
    var ts = moment(da[i].time,"YYYY-M-D H:mm:").utcOffset('+07:00');
    var dObj = new Date(ts);
    //alert(d);
    //var mmtMidnight = ts.clone().startOf('day');
    //var diffMinutes = ts.clone().diff(mmtMidnight, 'minutes');
    var a=parseInt(da[i].total);var b=parseInt(da[i][200]);var c=parseInt(da[i][400]);var d=parseInt(da[i][415]);var e=parseInt(da[i][500]);var f=parseInt(da[i][502]);var g=parseInt(da[i][503]);
    etotCount=etotCount+a;e200Count=e200Count+b;e400Count=e400Count+c;e415Count=e415Count+d;e500Count=e500Count+e;e502Count=e502Count+f;e503Count=e503Count+g;
    etot.push([dObj,a]);e200.push([dObj,b]);e400.push([dObj,c]);//ticks.push([ts,ts]);
    e415.push([dObj,d]);e500.push([dObj,e]);e502.push([dObj,f]);e503.push([dObj,g]);
  }
  data = [{ data:etot, label:"Total("+etotCount+")", lines:{show:true}, points:{ show: true}},{ data:e200, label:"200("+e200Count+")", lines:{show:true},color:"#46862b"},{ data:e400, label:"400("+e400Count+")", lines:{show:true}},{ data:e415, label:"415("+e415Count+")", lines:{show:true}},{ data:e500, label:"500("+e500Count+")", lines:{show:true},color:"#fa7e32"},{ data:e502, label:"502("+e502Count+")", lines:{show:true},color:"#d96e2c"},{ data:e503, label:"503("+e503Count+")", lines:{show:true},color:"#fa0000"}];
  options = {legend:{position:"se"},xaxis: {
                            axisLabelUseCanvas: true,
                            labelAngle: -90,
                            mode: "time", timeformat: "%H:%M",timezone: "browser"
                        },
                        yaxis: { axisLabelUseCanvas: true ,clickable: true,hoverable: true},
                        grid: { hoverable: true }};
        chart = $.plot($("#placeholder"),data,options);
console.log(etot);
    }

第一个.use('/public', express.static('public')) 是您在网址中用作前缀的内容。第二个public是文件系统中的物理文件夹。

另一种选择是不在您的网址中使用public

来自文档:

  

要为express.static函数提供的文件创建虚拟路径前缀(文件系统中实际不存在路径),请为静态目录指定安装路径,如下所示:

     

/public/*

     

现在,您可以从/ static路径前缀加载公共目录中的文件。