-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,47 @@ | ||
const createError = require('http-errors') | ||
const express = require('express') | ||
const path = require('path') | ||
const cookieParser = require('cookie-parser') | ||
const logger = require('morgan') | ||
const fileUpload = require('express-fileupload') | ||
const createError = require("http-errors"); | ||
const express = require("express"); | ||
const path = require("path"); | ||
const cookieParser = require("cookie-parser"); | ||
const logger = require("morgan"); | ||
const fileUpload = require("express-fileupload"); | ||
|
||
const indexRouter = require('./routes/index') | ||
const clientsRouter = require('./routes/clients') | ||
const torrentsRouter = require('./routes/torrents') | ||
const indexRouter = require("./routes/index"); | ||
const filesRouter = require("./routes/files"); | ||
const clientsRouter = require("./routes/clients"); | ||
const torrentsRouter = require("./routes/torrents"); | ||
|
||
const app = express() | ||
const app = express(); | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')) | ||
app.set('view engine', 'pug') | ||
app.set("views", path.join(__dirname, "views")); | ||
app.set("view engine", "pug"); | ||
|
||
app.use(logger('dev')) | ||
app.use(express.json()) | ||
app.use(express.urlencoded({ extended: false })) | ||
app.use(cookieParser()) | ||
app.use(fileUpload()) | ||
app.use(express.static(path.join(__dirname, 'public'))) | ||
app.use(logger("dev")); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(fileUpload()); | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
|
||
app.use('/', indexRouter) | ||
app.use('/api/v1/clients', clientsRouter) | ||
app.use('/api/v1/torrents', torrentsRouter) | ||
app.use("/", indexRouter); | ||
app.use("/api/v1/files", filesRouter); | ||
app.use("/api/v1/clients", clientsRouter); | ||
app.use("/api/v1/torrents", torrentsRouter); | ||
|
||
// catch 404 and forward to error handler | ||
app.use((req, res, next) => { | ||
next(createError(404)) | ||
}) | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use((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 : {} | ||
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') | ||
}) | ||
res.status(err.status || 500); | ||
res.render("error"); | ||
}); | ||
|
||
module.exports = app | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const express = require("express"); | ||
const router = express.Router(); | ||
|
||
const fileDir = | ||
process.env.NODE_ENV === "production" | ||
? process.env.APP_DIR | ||
: process.env.TMP_DIR; | ||
|
||
router.get("/:name", (req, res, next) => { | ||
if (!req.params || !req.params.name) { | ||
return next(new Error("No file name provided")); | ||
} | ||
|
||
const filePath = path.join(fileDir, req.params.name); | ||
|
||
if (!fs.existsSync(filePath)) { | ||
return next(new Error(`File "${filePath}" does not exist`)); | ||
} | ||
|
||
res.download(filePath); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters