2017-10-31 9 views
0

2つの異なるpugファイルを使用する必要があるプロジェクトをまとめると、その時刻に基づいて変更されます(night.pugは午後7時から7時まで有効です)。 :00amとday.pugは午前7時から午後7時まで有効です)Expressのページは時刻に基づいて変更されます

私はこれらのページをレンダリングする方法を教えてください。私は新しいDate()を実装することを知っています。getHours();システム時間を得るために、私はページを条件付きでレンダリングするつもりは全く分かりません。 2つの異なるapp.getが必要ですか?

'use strict'; 

const express = require('express'), 
    app = express(); 

app.use(express.static('resources')); 

app.set('view engine', 'pug'); 
app.set('views', './views'); 

app.get() 

const server = app.listen(3000, function() { 
    console.log(`Started server on port ${server.address().port}`); 
}); 

私はこれにどのようにアプローチするか分からないので、app.getは現在空です。どんな助けでも大歓迎です。

答えて

1

私はこれがこのようなものになると思います。それをテストしていないが、開始するのに役立つはずです。エクスプレスドキュメントページでコードを微調整しました。

app.get('/', function (req, res) { 
    // Get the current hour (you might need to do the UTC thing) 
    const currentHour = (new Date()).getHours(); 
    // between 7am and 7pm on a 24 hour clock 
    const isDay = currentHour > 7 && currentHour < 19; 
    // Determine which template to render 
    if (isDay) { 
    res.render('day', { title: 'Hey', message: 'Hello there!' }); 
    } else { 
    res.render('night', { title: 'Hey', message: 'Hello there!' }); 
    } 
}); 

ドキュメント:https://expressjs.com/en/guide/using-template-engines.html

関連する問題