Commit ed11c327 authored by David Lay's avatar David Lay

added file listing route

parent 8884c1f1
......@@ -5,6 +5,7 @@ var bodyParser = require('body-parser')
var index = require('./routes/index')
var upload = require('./routes/upload')
var files = require('./routes/files')
var app = express()
......@@ -19,6 +20,7 @@ app.use(express.static(path.join(__dirname, 'public')))
app.use('/', index)
app.use('/upload', upload)
app.use('/files', files)
// catch 404 and forward to error handler
app.use(function (req, res, next) {
......
var express = require('express')
var fs = require('fs')
var path = require('path')
var util = require('util')
var router = express.Router()
function filterDotFiles (files) {
return files.filter(f => f.match(/^[^.].*$/))
}
router.get('/', function (req, res, next) {
const testFolder = path.join(__dirname, '../public/uploads')
var readdir = util.promisify(fs.readdir)
return readdir(testFolder)
.then(filterDotFiles)
.then(files => {
res.render('files.ejs', {files})
})
.catch(next)
})
module.exports = router
......@@ -3,7 +3,7 @@ var router = express.Router()
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' })
res.render('index', { title: 'Express file Upload Demo' })
})
module.exports = router
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='/stylesheets/style.css' />
<title>Existing Files</title>
</head>
<body>
<h1>Files in alphabetic order:</h1>
<ul>
<% if (files.length === 0) { %>
<li><em>No files found</em></li>
<% } else { %>
<% files.forEach(file => { %>
<li><a href="/uploads/<%= file %>"><%= file %></a></li>
<% }) %>
<% } %>
</ul>
<br/>
<br/>
<a href="/">Go Back</a>
</body>
</html>
\ No newline at end of file
......@@ -7,5 +7,10 @@
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<a href="/upload"> Go to form</a>
<br/><br/>
<a href="/files">Browse existing files</a>
</body>
</html>
......@@ -14,5 +14,8 @@
<button type="submit">Send</button>
</form>
<br/>
<br/>
<a href="/">Go Back</a>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment