const chokidar = require('chokidar'); const fs = require('fs'); const path = require('path'); var createError = require('http-errors'); var express = require('express'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); var apiRouter = require('./routes/api'); var config = require("./config.json"); const watcher = chokidar.watch(config.DXMemorialImageDirectory, { persistent: true }); watcher .on('add', (filePath) => { const isImage = /\.(jpe?g|png|gif)$/i.test(filePath); if (isImage) { const fileName = path.basename(filePath); const destPath = path.join(config.imageFolder, fileName); fs.access(destPath, (err) => { if (err) { fs.copyFile(filePath, destPath, (err) => { if (err) throw err; console.log(`${fileName} Imported.`); }); } else { } }); } }) .on('error', (error) => { console.error(`Watcher error: ${error}`); }); // readFileSync function must use __dirname get current directory // require use ./ refer to current directory. const options = { }; var app = express() // Create HTTPs server. //var httpsServer = https.createServer(options, app); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(express.static(__dirname + '/public')); app.use(logger('dev')); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.json({ extended: false })); app.use('/', indexRouter); app.use('/api/', apiRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); }); var PORT = process.env.PORT || 8000 // 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'); }); app.listen(PORT, () => console.log(`Server is running in port ${PORT}`)); module.exports = app;