Parcourir la source

Exported functions to route files

rigwild il y a 5 ans
Parent
commit
f65a1d3526
3 fichiers modifiés avec 26 ajouts et 29 suppressions
  1. 0 21
      api/functions.js
  2. 2 2
      api/index.js
  3. 24 6
      api/routes/listScenes.js

+ 0 - 21
api/functions.js

@@ -1,9 +1,5 @@
 'use strict'
 
-import _fs, { promises as fs } from 'fs'
-import boom from 'boom'
-import { imagesPath } from '../config'
-
 /**
  * Call the error handler if a middleware function throw an error
  *
@@ -28,20 +24,3 @@ export const errorHandler = (err, req, res, next) => {
   console.error(`Error ${payload.statusCode} - ${payload.error}\n${payload.message}\n`)
   return res.status(payload.statusCode).json(payload)
 }
-
-/**
- * Get a list of all available scenes
- *
- * @returns {string[]} the available scenes
- */
-export const getAvailableScenes = async () => {
-  try {
-    // Check if the images directory exists
-    await fs.access(imagesPath, _fs.constants.R_OK)
-  }
-  catch (err) {
-    // The images directory does not exist or is not accessible
-    throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
-  }
-  return fs.readdir(imagesPath)
-}

+ 2 - 2
api/index.js

@@ -5,6 +5,7 @@ import express from 'express'
 import compression from 'compression'
 import serveStatic from 'serve-static'
 import helmet from 'helmet'
+import cors from 'cors'
 
 import routes from './routes'
 import { errorHandler } from './functions'
@@ -26,13 +27,12 @@ if (serveClient) {
 else {
   // Don't serve client files (Client is remote)
   // Turn "Cross-origin resource sharing" on to allow the remote client to connect to the API
-  import('cors').then(({ default: cors }) => app.use(cors()))
+  app.use(cors())
 }
 
 // Serve images. "serve-static" is used because it caches images ("express.static" doesn't)
 app.use(serveStatic(imagesPath))
 
-
 // Load all the API routes in the server
 app.use(apiPrefix, routes)
 

+ 24 - 6
api/routes/listScenes.js

@@ -1,14 +1,32 @@
 'use strict'
 
 import express from 'express'
-import { asyncMiddleware, getAvailableScenes } from '../functions'
+import _fs, { promises as fs } from 'fs'
+import boom from 'boom'
+import { imagesPath } from '../config'
+import { asyncMiddleware } from '../functions'
 
 const router = express.Router()
 
-router.get('/', asyncMiddleware(async (req, res) => {
-  const dirContent = await getAvailableScenes()
-  console.log('triggered')
-  res.json(dirContent)
-}))
+// Route which returns a list of all available scenes in the `imagesPath` directory
+
+/**
+ * Get a list of all available scenes
+ *
+ * @returns {string[]} the available scenes
+ */
+const getAvailableScenes = async () => {
+  try {
+    // Check if the directory which contains images exists
+    await fs.access(imagesPath, _fs.constants.R_OK)
+  }
+  catch (err) {
+    // The images directory does not exist or is not accessible
+    throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
+  }
+  return fs.readdir(imagesPath)
+}
+
+router.get('/', asyncMiddleware(async (req, res) => res.json(await getAvailableScenes())))
 
 export default router