Split index file into src/ folder components
This commit is contained in:
		
							
								
								
									
										15
									
								
								src/app.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/app.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| const express = require("express"); | ||||
|  | ||||
| const asciiFontController = require("./controllers/asciiController.js"); | ||||
| const modtFontController = require("./controllers/motdController.js"); | ||||
| const requiredQueryMiddleware = require("./requiredQueryMiddleware"); | ||||
|  | ||||
| const app = express(); | ||||
| const port = 3000; | ||||
|  | ||||
| app.get("/ascii", requiredQueryMiddleware, asciiFontController); | ||||
| app.get("/motd", requiredQueryMiddleware, modtFontController); | ||||
|  | ||||
| app.listen(port, () => { | ||||
|   console.log(`Font generation application listening on port ${port}.`); | ||||
| }); | ||||
							
								
								
									
										33
									
								
								src/ascii.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/ascii.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| const figlet = require("figlet"); | ||||
|  | ||||
| const defaultFontOptions = { | ||||
|   font: "Larry 3D", | ||||
|   horizontalLayout: "default", | ||||
|   verticalLayout: "default", | ||||
|   width: 80, | ||||
|   whitespaceBreak: true, | ||||
| }; | ||||
|  | ||||
| function generateAscii(message, options) { | ||||
|   const _options = { | ||||
|     ...defaultFontOptions, | ||||
|     ...options, | ||||
|   }; | ||||
|  | ||||
|   return new Promise((resolve, reject) => { | ||||
|     figlet.text(message, _options, (err, data) => { | ||||
|       if (err) { | ||||
|         console.error("error from figlet:", error); | ||||
|         return reject(err); | ||||
|       } | ||||
|  | ||||
|       if (_options.font === "Larry 3D") { | ||||
|         data = data.replaceAll("L", "_"); | ||||
|       } | ||||
|  | ||||
|       resolve(data); | ||||
|     }); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports = generateAscii; | ||||
							
								
								
									
										16
									
								
								src/controllers/asciiController.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/controllers/asciiController.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| const generateAscii = require("../ascii.js"); | ||||
|  | ||||
| const asciiFontController = (req, res) => { | ||||
|   const { text } = req.query; | ||||
|  | ||||
|   return generateAscii(text) | ||||
|     .then((ascii) => res.send(ascii)) | ||||
|     .catch((error) => | ||||
|       res.status(error?.statusCode || 500).send({ | ||||
|         success: false, | ||||
|         message: error?.message || "Unexpected error from font generation", | ||||
|       }) | ||||
|     ); | ||||
| }; | ||||
|  | ||||
| module.exports = asciiFontController; | ||||
							
								
								
									
										17
									
								
								src/controllers/motdController.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/controllers/motdController.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| const generateAscii = require("../ascii.js"); | ||||
|  | ||||
| const modtFontController = (req, res) => { | ||||
|   const { text } = req.query; | ||||
|  | ||||
|   return generateAscii(message) | ||||
|     .then((ascii) => generateCatOutput(ascii)) | ||||
|     .then((motd) => res.send(motd)) | ||||
|     .catch((error) => { | ||||
|       return res.status(500).send({ | ||||
|         success: false, | ||||
|         message: "Unexpected error from font generation", | ||||
|       }); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| module.exports = modtFontController; | ||||
							
								
								
									
										13
									
								
								src/errors.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/errors.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| class MissingTextError extends Error { | ||||
|   constructor(error = null) { | ||||
|     const message = `Missing query parameter 'text'`; | ||||
|     super(message); | ||||
|  | ||||
|     this.error = error; | ||||
|     this.statusCode = 400; | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   MissingTextError, | ||||
| }; | ||||
							
								
								
									
										14
									
								
								src/requiredQueryMiddleware.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/requiredQueryMiddleware.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| const { MissingTextError } = require("./errors.js"); | ||||
|  | ||||
| const requiredQueryMiddleware = (req, res, next) => { | ||||
|   const { text } = req.query; | ||||
|  | ||||
|   if (!text || text?.length === 0) { | ||||
|     const error = new MissingTextError(); | ||||
|     return res.status(error?.statusCode || 500).send(error?.message); | ||||
|   } | ||||
|  | ||||
|   next(); | ||||
| }; | ||||
|  | ||||
| module.exports = requiredQueryMiddleware; | ||||
		Reference in New Issue
	
	Block a user