Guest User

Untitled

a guest
Jun 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. /**
  2. * Server side for issuance client scripts
  3. */
  4.  
  5. import path from "path";
  6. import Express from 'express';
  7. import BodyParser from "body-parser";
  8.  
  9. import { readFile, writeFile } from "./util";
  10.  
  11. const root = process.cwd();
  12. const p = path.resolve(root, 'build');
  13. const configFile = path.resolve(root, 'config/config.json');
  14. const app = Express();
  15.  
  16. app.use(Express.static(p));
  17. app.use(BodyParser.urlencoded({ extended: false }));
  18. app.use(BodyParser.json());
  19.  
  20.  
  21. app.get('/', (req, res) => {
  22. res.sendFile(`${p}/index.html`, {
  23. headers: {
  24. 'Content-Type': 'text/html'
  25. }
  26. });
  27. });
  28.  
  29. app.post('/active', async (req, res) => {
  30. const { active } = req.body;
  31.  
  32. try {
  33. await writeFile(configFile, JSON.stringify({ active }));
  34. } catch (error) {
  35. throw error;
  36. res.status(500).send(error);
  37. }
  38.  
  39. res.status(200).send('Ok');
  40. });
  41. app.get('/active', async (req, res) => {
  42. let active;
  43.  
  44. try {
  45. active = JSON.parse(await readFile(configFile));
  46. } catch (error) {
  47. throw error;
  48. res.status(500).send(error);
  49. }
  50.  
  51. res.status(200).send(active);
  52. });
  53.  
  54.  
  55. app.listen(process.env.PORT, (err) => {
  56. if (err) throw err;
  57.  
  58. console.log(`Server is running on localhost:${process.env.PORT}`);
  59. });
Add Comment
Please, Sign In to add comment