server.js 2.12 KB
"use strict";

const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
const fs = require('fs');

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();

const dataFolder = './tensorflow/data';
const testFolder = './tensorflow/test';


app.set('view engine', 'pug');
app.set('views', './views');
app.locals.pretty = true

app.use(bodyParser.urlencoded({extended:false}));


// Redirect Root to Home
app.get('/', (req, res) => {
	res.redirect('/home');
});


// Main Page
app.get('/home', (req, res) => {
	fs.readdir(dataFolder, function(error, filelist){
		if(error)
			console.log(error);
		res.render('home', {fileList:filelist});
	});
});


// Directory existence checking
app.post('/directory_check', (req, res) => {
	
	var dir = req.body.directoryName;
	
	// Directory exists
	if(fs.existsSync(dataFolder + '/' + dir))
	{
		// Go back page
		res.render('error_directoryAdd');
	}
	// Directory doesn't exist
	else
	{
		// Make directory
		fs.mkdirSync(dataFolder + '/' + dir);
		console.log('디렉토리: ' + dir + ' 생성 완료');
		res.redirect('/home/' + dir);
	}
});


// Basic Directory Page
app.get('/home/:directoryName', (req, res) => {
	
});


// Image Upload Directory Page
app.get('/home/:directoryName/upload', (req, res) => {
	var upload_data = multer({ dest: 'tensorflow/data/' + directoryName });
	upload_data.array('ImageData')
	var imgFileArr = req.files;
});


// Delete Directory
app.get('/home/:directoryName/delete', (req, res) => {
	// exist query.real
	var directoryName = req.params.directoryName;
	if (req.query.real) {
		// Remove Directory and Files
		var path = dataFolder + '/' + directoryName;
		fs.readdirSync(path).forEach(function(file,index){
			var curPath = path + "/" + file;
			fs.unlinkSync(curPath);
		});
		fs.rmdirSync(path);
		res.redirect('/');
	}
	else {
		res.render('directoryDeleteCheck');
	}
});


// Image Test Page
app.post('/test',  (req, res) => {
	var upload_test = multer({ dest: 'tensorflow/test/' });
	upload_test.single('ImageTest')
	var imgTest = req.file;
});


app.listen(PORT, HOST);
console.log('Running on http://${HOST}:${POST}');