无法使用Express和NightmareJS加载应用程序

时间:2017-06-26 18:17:53

标签: express nightmare

我正在使用Express-Generator文件结构和NightmareJS开发一个小应用程序,所以我正在使用路由。 Nightmare函数位于routes目录内的index.js文件中,Nightmare中的wait()函数设置为8000,因此等待8秒才会加载右元素。一切看起来都很好但是当我运行应用程序时它无法找到索引文件并显示404 Not Found页面,但如果我等待8秒并刷新页面则可以正常工作。 我做了另一个没有路由的测试,我的意思是,只在app.js文件中工作并且工作正常,页面等待加载时间然后显示结果,这让我觉得我在配置错误了路线文件?有任何想法吗?谢谢。

这是我的代码:

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var allday = require('./routes/allday');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('view options', {layout:false});

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/allday', allday);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

var express = require('express');
var router = express.Router();
var Nightmare = require('nightmare');
var nightmare = Nightmare();
var url = 'http://portus.puertos.es/Portus_RT/table.html?&t=1&p=MjA1NjA4Mjs7LTIuMzMyOTk5OTQ0Njg2ODg5Njs7MzYuODMzMDAwMTgzMTA1NDc7O1dBTkE7OzA7OzIwNTYwODI7O1dBTkFfREFUQTs7OztNRURJVEVSUkFORU87OzA7OzEyOztNRUQ=&locale=es'

nightmare
  .goto(url)
  .wait(8000)
  .evaluate(function () {
    var currentData = [];
    var dayData = [];
    var dir_waves = '';
    var checker = '';
    var size = '.gwt-TabPanelBottom>div>table>tbody>tr>td:nth-of-type(2)>table>tbody>tr:nth-of-type(10)>td:nth-of-type';
    var period = '.gwt-TabPanelBottom>div>table>tbody>tr>td:nth-of-type(2)>table>tbody>tr:nth-of-type(12)>td:nth-of-type';
    var direction = '.gwt-TabPanelBottom>div>table>tbody>tr>td:nth-of-type(2)>table>tbody>tr:nth-of-type(11)>td:nth-of-type';
    var wind = '.gwt-TabPanelBottom>div>table>tbody>tr>td:nth-of-type(2)>table>tbody>tr:nth-of-type(2)>td:nth-of-type';
    var time = '.gwt-TabPanelBottom>div>table>tbody>tr>td:nth-of-type(2)>table>tbody>tr:nth-of-type(1)>td:nth-of-type';

    for(var i = 7; i <= 21; i++) {
      var size_waves = document.querySelector(size + '(' + i + ')').innerText;
      var period_waves = document.querySelector(period + '(' + i + ')').innerText;
      var direction_waves = document.querySelector(direction + '(' + i + ')' + '>div').title;
      var wind_vel = document.querySelector(wind + '(' + i + ')').innerText;
      var hour = document.querySelector(time + '(' + i + ')').innerText;

      //Preparing data to show

      //Asking just numbers for degrees
      direction_waves = direction_waves.replace(/[^0-9]/gi, '');

      // Translating numbers into coordinates
      if (direction_waves == 0) {
        dir_waves = 'N';
      } else if(direction_waves > 0 || direction_waves < 90){
        dir_waves = 'NE';
      } else if(direction_waves == 90){
        dir_waves = 'E';
      } else if(direction_waves > 90 || direction_waves < 180){
        dir_waves = 'SE';
      } else if(direction_waves == 180){
        dir_waves = 'S';
      } else if(direction_waves > 180 || direction_waves < 270){
        dir_waves = 'SO';
      } else if(direction_waves == 270){
        dir_waves = 'O';
      } else if(direction_waves > 270 || direction_waves < 360){
        dir_waves = 'NO';
      } else {
        dir_waves = 'N';
      }

      //All day data
      dayData.push([hour, size_waves, period_waves, direction_waves, dir_waves, wind_vel]);

      //Checking current hour for data
      Date.prototype.timeNow = function () {
        return ((this.getHours() < 10)?"0":"") + this.getHours();
      }
      var dateTime = new Date().timeNow();

      //Current time data
      if (dateTime == hour) {
        currentData.push([hour, size_waves, period_waves, direction_waves, dir_waves, wind_vel]);
      }

      //Surf checking
      if (currentData[1] > 0.6 && currentData[2] >= 6 && currentData[3] <= 200 && currentData[5] <= 5) {
        checker = ["SURF'S UP!", "thumbs-up"];
      } else {
        checker = ["SURF'S DOWN", "thumbs-down"];
      }
    }
    return [dayData, currentData, checker];

  })
  .end()
  .then(function (result) {
    console.log(result);
    router.get('/', function(req, res, next){
      res.render('index', {data: result});
    });
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

module.exports = router;

0 个答案:

没有答案