|
| 1 | +const cmd = require('chronos-microservice-debugger2'); |
| 2 | + |
| 3 | +cmd.propagate(); |
| 4 | +const PORT = 4545; |
| 5 | +const express = require('express'); |
| 6 | +const path = require('path'); |
| 7 | +const cors = require('cors'); |
| 8 | + |
| 9 | +const app = express(); |
| 10 | +const bodyParser = require('body-parser'); |
| 11 | +const controller = require('./BookController.js'); |
| 12 | + |
| 13 | +// we're using the chronos debugger tool here to intercept |
| 14 | +// request and propagate our context onto said request as it travels |
| 15 | + |
| 16 | +app.use('/', cmd.microCom('books_microservice', 'sql', 'postgres://tsfcbdjo:[email protected]:5432/tsfcbdjo')); |
| 17 | +cmd.microHealth('books_microservice', 'sql', 'postgres://tsfcbdjo:[email protected]:5432/tsfcbdjo', 'h'); |
| 18 | + |
| 19 | +app.use(bodyParser.json()); |
| 20 | +app.use(cors()); |
| 21 | +app.use('/', express.static(path.resolve(__dirname, '../frontend'))); |
| 22 | + |
| 23 | +// ********** I PROBABLY STILL NEED THIS PART FOR CHRONOS TO WORK AND DEBUG MY MICOSERVICE ************* |
| 24 | +// This requires the chronos middleware into the server ***Tim's comment |
| 25 | +// const mw = require('../mwMongo.js'); |
| 26 | + |
| 27 | +// app.use('/', mw.microCom(path.basename(__filename))); |
| 28 | + |
| 29 | +// CHAOS FLOW - SIMPLY A TEST FOR THE EXPESS SERVER |
| 30 | +// app.use((req, res, next) => { |
| 31 | +// console.log( |
| 32 | +// `*************************************************************************************** |
| 33 | +// CHAOS FLOW TEST --- METHOD:${req.method}, PATH: ${ |
| 34 | +// req.url |
| 35 | +// }, BODY: ${JSON.stringify(req.body)}, ID: ${req.query.id} |
| 36 | +// ***************************************************************************************`, |
| 37 | +// ); |
| 38 | +// next(); |
| 39 | +// }); |
| 40 | + |
| 41 | +// This route will create a new book! |
| 42 | +app.post('/createbook', controller.createBook, (req, res) => { |
| 43 | + res.status(200).json(res.locals.createBook); |
| 44 | +}); |
| 45 | + |
| 46 | +// This route will delete a book |
| 47 | +app.delete('/deletebook:id?', controller.deleteBook, (req, res) => { |
| 48 | + res.status(200).json(res.locals.deleteBook); |
| 49 | +}); |
| 50 | + |
| 51 | +// This route will get all the books in the database |
| 52 | +app.get('/getbooks', controller.getBooks, (req, res) => { |
| 53 | + res.status(200).json(res.locals.getBooks); |
| 54 | +}); |
| 55 | + |
| 56 | +// This route gets orders from the Orders application |
| 57 | +app.get('/getordersinfo', controller.getorderinfo, (req, res) => { |
| 58 | + res.status(200).json(res.locals.getorderinfo); |
| 59 | +}); |
| 60 | + |
| 61 | + |
| 62 | +// This is my global error handler - isn't being used right now and it's not breaking anything so... |
| 63 | +function errorHandler(error, req, res, next) { |
| 64 | + // console.log(err.stack); |
| 65 | + const defaultErr = { |
| 66 | + log: 'Express error handler caught unknown middleware error', |
| 67 | + status: 400, |
| 68 | + message: { err: 'An error occurred' }, |
| 69 | + }; |
| 70 | + const errorObj = Object.assign(defaultErr, error); |
| 71 | + console.log(`Here is the error object's response: ${errorObj.log}`); |
| 72 | + |
| 73 | + res.status(errorObj.status).json(errorObj.message); |
| 74 | +} |
| 75 | + |
| 76 | +// Open and listen to server on said port |
| 77 | +app.listen(PORT, () => { |
| 78 | + console.log(`Book server running on port ${PORT} ...`); |
| 79 | +}); |
0 commit comments